How can I help you?
Row selection action in gantt control
3 Jan 20248 minutes to read
The row selection in the Gantt control can be enabled or disabled using the AllowSelection property. You can get the selected row object using the getSelectedRecords method. The following code example shows how to disable the row selection in Gantt.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="false">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
</ejs-gantt>public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}NOTE
Rowselection is the default type of Gantt selection mode.
Selecting a row on initial load
You can select a row at the time of loading by setting the index of the row to the SelectedRowIndex property. Find the following code example for details.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true" selectedRowIndex="3">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
</ejs-gantt>public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Selecting a row dynamically
You can select a single row dynamically using the selectRow method. Similarly, you can use the selectRows method to dynamically select multiple rows. The following code demonstrates how to select a single or multiple rows dynamically by clicking the custom button.
<ejs-button id="selectRow" content="Select Row" cssClass="e-primary"></ejs-button>
<ejs-button id="selectRows" content="Select Rows" cssClass="e-primary"></ejs-button>
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-selectionsettings mode="Row" type="Multiple"></e-gantt-selectionsettings>
</ejs-gantt>
<script>
document.getElementById('selectRow').addEventListener('click', function (args) {
var ganttObj = document.getElementById('Gantt').ej2_instances[0];
ganttObj.selectionModule.selectRow(2); // passing the record index to select the row
});
document.getElementById('selectRows').addEventListener('click', function (args) {
var ganttObj = document.getElementById('Gantt').ej2_instances[0];
ganttObj.selectionModule.selectRows([1, 2, 3]); // passing the record index to select the rows
});
</script>public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}Multiple row selection
You can select multiple rows by setting the SelectionSettings.Type property to Multiple. You can select more than one row by holding down the CTRL key while selecting multiple rows. The following code example explains how to enable multiple selection in Gantt.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-selectionSettings mode="Row" type="Multiple"></e-gantt-selectionSettings>
</ejs-gantt>public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}Customize row selection action
While selecting a row in Gantt, the RowSelecting and RowSelected events will be triggered. The the RowSelecting event will be triggered on initialization of row selection, and you can get the previously selected row and current selecting row’s information, which is used to prevent selection of a particular row. The RowSelected event will be triggered on completion of row selection action, and you can get the current selected row’s information through this event. The following code example demonstrates how to prevent the selection of a row using the RowSelecting event.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true" rowSelecting="rowSelecting">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
</ejs-gantt>
<script>
function rowSelecting(args) {
if (args.rowIndex == 3) {
args.cancel = true;
}
}
</script>public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}In the Gantt control, when you click an already selected row, selection will be cleared. While deselecting a row in Gantt, the RowDeselecting and RowDeselected events will occur. The RowDeselecting event will occur on initialization of deselecting row, and you can get the current deselecting row’s information to prevent the deselection of particular row. The RowDeselected event will occur on completion of row deselection action, and you can get the current deselected row’s information.