Search results

Rows in ASP.NET Core Gantt control

19 Oct 2021 / 5 minutes to read

Row represents a task information from the data source, and it is possible to perform the following actions in Gantt rows.

Row height

It is possible to change the height of the row in Gantt by setting row height in pixels to the RowHeight property. The following code example explains how to change the row height in Gantt at load time.

tagHelper
rowHeight.cs
Copied to clipboard
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" rowHeight="60">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
</ejs-gantt>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Expand/Collapse Row

In Gantt parent tasks are expanded/collapsed by using expand/collapse icons, expand all/collapse all toolbar items and by using public methods. By default all tasks in Gantt was rendered in expanded state but we can change this status in Gantt.

Collapse all tasks at Gantt load

All tasks available in Gantt was rendered in collapsed state by setting CollapseAllParentTasks property as true. The following code example shows how to use this property.

tagHelper
enableCollapseAll.cs
Copied to clipboard
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" collapseAllParentTasks="true">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
</ejs-gantt>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Define expand/collapse status of tasks

In Gantt, we can render some tasks in collapsed state and some tasks in expanded state, this can done by defining expand status of the task in data source. This value was mapped to Gantt control by using ExpandState property. The following code example shows how to use this property.

tagHelper
expandState.cs
Copied to clipboard
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks" expandState="isExpand">
                    </e-gantt-taskfields>
                    
                </ejs-gantt>
Copied to clipboard
public IActionResult Index()
{
     ViewBag.DataSource = ProjectNewData();
     return View();
}

public static List<GanttDataSource> ProjectNewData()
        {
            List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

            GanttDataSource Record1 = new GanttDataSource()
            {
                TaskId = 1,
                TaskName = "Project Initiation",
                StartDate = new DateTime(2019, 04, 02),
                EndDate = new DateTime(2019, 04, 21),
                SubTasks = new List<GanttDataSource>(),
                isExpand = true
            };

            GanttDataSource Child1 = new GanttDataSource()
            {
                TaskId = 2,
                TaskName = "Identify Site location",
                StartDate = new DateTime(2019, 04, 02),
                Progress = 50,
                Duration = 3
            };

            GanttDataSource Child2 = new GanttDataSource()
            {
                TaskId = 3,
                TaskName = "Perform Soil test",
                StartDate = new DateTime(2019, 04, 02),
                Duration = 3,
                Progress = 50,
               
            };

            GanttDataSource Child3 = new GanttDataSource()
            {
                TaskId = 4,
                TaskName = "Soil test approval",
                StartDate = new DateTime(2019, 04, 02),
                Progress = 50,
                Duration = 3,
               
            };
            Record1.SubTasks.Add(Child1);
            Record1.SubTasks.Add(Child2);
            Record1.SubTasks.Add(Child3);

            GanttDataSource Record2 = new GanttDataSource()
            {
                TaskId = 5,
                TaskName = "Project Estimation",
                StartDate = new DateTime(2019, 04, 02),
                Duration = 0,
                Progress = 50,
                SubTasks = new List<GanttDataSource>(),
                isExpand = false

            };
            GanttDataSource Child4 = new GanttDataSource()
            {
                TaskId = 6,
                TaskName = "Develop floor plan for estimation",
                StartDate = new DateTime(2019, 04, 04),
                Duration = 4,
                Progress = 50,
                EndDate = new DateTime(2019, 04, 21),
                SubTasks = new List<GanttDataSource>(),
            };

            GanttDataSource Child5 = new GanttDataSource()
            {
                TaskId = 7,
                TaskName = "List materials",
                StartDate = new DateTime(2019, 04, 04),
                Duration = 4,
                Progress = 50,
                
            };

            Record2.SubTasks.Add(Child4);
            Record2.SubTasks.Add(Child5);

            GanttDataSourceCollection.Add(Record1);
            GanttDataSourceCollection.Add(Record2);

            return GanttDataSourceCollection;
        }

        public class GanttDataSource
        {
            public int TaskId { get; set; }
            public string TaskName { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public int? Duration { get; set; }
            public int Progress { get; set; }
            public bool isExpand { get; set; }
            public List<GanttDataSource> SubTasks { get; set; }
        }

Alt text

Customize expand/collapse action

On expand action Expanding and Expanded event will be triggered with current expanding row’s information. Similarly on collapse action Collapsing and Collapsed event will be triggered. Using this events and it’s arguments we can customize the expand/collapse action. The following code example shows how to prevent the particular row from expand/collapse action using Expanding and Collapsing event.

tagHelper
expandCollapseEvent.cs
Copied to clipboard
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" collapsing="collapsing" expanding="expanding">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
                    
</ejs-gantt>
				
	<script>
        function collapsing(args) {
            if (args.data.TaskId == 1) // we can't collapse Task Id 1
                args.cancel = true; 
        }
        function expanding(args) {
            if (args.data.TaskId == 5) // we can't expand Task Id 5
                args.cancel = true;
        }
    </script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Drag and drop

You can dynamically rearrange the rows in the Gantt control by using the AllowRowDragAndDrop property. Using this property, row drag and drop can be enabled or disabled in Gantt. Using this feature, rows can be dropped at above and below as a sibling or child to the existing rows

tagHelper
defaultdrag.cs
Copied to clipboard
<ejs-gantt id='DragAndDrop' dataSource="ViewBag.dataSource" height="450px" allowRowDragAndDrop="true" highlightWeekends="true" treeColumnIndex="1"
            projectStartDate="03/24/2019" projectEndDate="07/06/2019">
        <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" endDate="EndDate" duration="Duration" progress="Progress" dependency="Predecessor"
            child="SubTasks">
        </e-gantt-taskfields>
   </ejs-gantt>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Multiple row drag and drop

Gantt also supports dragging multiple rows at a time and drop them on any rows above, below, or at child positions. In Gantt, you can enable the multiple drag and drop by setting the SelectionSettings.Type to Multiple and you should enable the AllowRowDragAndDrop property.

tagHelper
multidrag.cs
Copied to clipboard
<ejs-gantt id='DragAndDrop' dataSource="ViewBag.dataSource" height="450px" allowRowDragAndDrop="true" highlightWeekends="true" treeColumnIndex="1"
            projectStartDate="03/24/2019" projectEndDate="07/06/2019">
        <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" endDate="EndDate" duration="Duration" progress="Progress" dependency="Predecessor"
                child="SubTasks">
        </e-gantt-taskfields>
        <e-gantt-selectionsettings type="Multiple"></e-gantt-selectionsettings>
  </ejs-gantt>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Drag and drop events

We provide various events to customize the row drag and drop action, the following table explains about the available events and its details.

Event Name Description
RowDragStartHelper Triggers when clicking the drag icon or Gantt row.
RowDragStart Triggers when drag action starts in Gantt.
RowDrag Triggers while dragging the Gantt row.
RowDrop Triggers when a drag row was dropped on the target row.

Customize row drag and drop action

In Gantt, the RowDragStartHelper and RowDrop events are triggered on row drag and drop action. Using this event, you can prevent dragging of particular record, validate the drop position, and cancel the drop action based on the target record and dragged record. The following topics explains about this.

Prevent dragging of particular record

You can prevent drag action of the particular record by setting the cancel property to true, which is available in the RowDragStartHelper event argument based on our requirement. In the following sample, drag action was restricted for first parent record and its child records.

tagHelper
preventdrag.cs
Copied to clipboard
<ejs-gantt id='DragAndDrop' dataSource="ViewBag.dataSource" height="450px" rowDragStartHelper="rowDragStartHelper" allowRowDragAndDrop="true" highlightWeekends="true" treeColumnIndex="1"
        projectStartDate="03/24/2019" projectEndDate="07/06/2019">
        <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" endDate="EndDate" duration="Duration" progress="Progress" dependency="Predecessor"
            child="SubTasks">
        </e-gantt-taskfields>
    </ejs-gantt>
	
	<script>
       function rowDragStartHelper(args) {
           var record = args.data[0] ? args.data[0] : args.data;
           var taskId = record.ganttProperties.taskId;
           if (taskId <= 4) {
               args.cancel = true;
           }
       }
	</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Validating drop position

You can prevent drop action based on the drop position and target record, by this, you can prevent dropping particular task on a specific task or specific position. This can be achieved by setting the cancel property to true, which is available in the RowDrop event argument.

In the following sample, we have prevented the drop action based on the position. In this sample, you cannot drop row as child in any of the available rows.

tagHelper
preventdrop.cs
Copied to clipboard
<ejs-gantt id='DragAndDrop' dataSource="ViewBag.dataSource" height="450px" rowDrop="rowDrop" allowRowDragAndDrop="true" highlightWeekends="true" treeColumnIndex="1"
        projectStartDate="03/24/2019" projectEndDate="07/06/2019">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" endDate="EndDate" duration="Duration" progress="Progress" dependency="Predecessor"
            child="SubTasks">
    </e-gantt-taskfields>
</ejs-gantt>

<script>
        function rowDrop(args) {
            if (args.dropPosition == "middleSegment") {
                args.cancel = true;
            }
        }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Perform row drag and drop action programmatically

Gantt provides option to perform row drag and drop action programmatically by using the reorderRows method, this method can be used for any external actions like button click. The following arguments are used to specify the positions to drag and drop a row:

  • fromIndexes: Index value of source(dragging) row.
  • toIndex: Value of target index.
  • position: Drop positions such as above, below, or child.

The following code example shows how to drag and drop a row on button click action.

tagHelper
dynamicdrag.cs
Copied to clipboard
<ejs-button id="DynamicDrag" content="Drop records as child" cssClass="e-primary"></ejs-button>
        <ejs-gantt id='DragAndDrop' dataSource="ViewBag.dataSource" height="450px" allowRowDragAndDrop="true" highlightWeekends="true" treeColumnIndex="1"
                projectStartDate="03/24/2019" projectEndDate="07/06/2019">
            <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" endDate="EndDate" duration="Duration" progress="Progress" dependency="Predecessor"
                child="SubTasks">
            </e-gantt-taskfields>
        </ejs-gantt>
		
		<script>
		    document.getElementById('DynamicDrag').addEventListener('click', function () {
			    var ganttObj = document.getElementById('DragAndDrop').ej2_instances[0];
                ganttObj.reorderRows([1, 2, 3], 4, 'child');
            });
		</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Customize rows

You can customize the appearance of a row in grid side, by using the rowDataBound event and in chart side by using queryTaskbarInfo event

tagHelper
customizeRows.cs
Copied to clipboard
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" queryTaskbarInfo="queryTaskbarInfo" rowDataBound="rowDataBound">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
</ejs-gantt>

    <script>
        function rowDataBound(args) {
            if (args.data['TaskId'] == 4) {
                args.row.style.background = 'cyan';
            }
        }
        function queryTaskbarInfo(args) {
            if (args.data['TaskId'] == 4) {
                args.rowElement.style.background = 'cyan';
            }
        }
       </script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Styling alternate rows

You can change the background colour of alternative rows in Gantt chart, by overriding the class as shown below.

Copied to clipboard
.e-altrow, tr.e-chart-row:nth-child(even)  {
background-color: #f2f2f2;
}
tagHelper
styleAlternateRows.cs
Copied to clipboard
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
</ejs-gantt>

    <style>
        .e-altrow, tr.e-chart-row:nth-child(even)  {
          background-color: #f2f2f2;
      }
    </style>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Row spanning

Gantt chart has an option to span row cells. You can achieve this using rowSpan attribute to span cells in the QueryCellInfo event.

In the following demo, Soil test approval cell is spanned to two rows in the TaskName column.

tagHelper
rowSpanning.cs
Copied to clipboard
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" queryCellInfo="queryCellInfo">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
</ejs-gantt>

    <script>
        function queryCellInfo(args) {
            if (args.data['TaskId'] == 4 && args.column.field === 'TaskName') {
                args.rowSpan = 2;
            }
        }
       </script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text