Rows in ASP.NET CORE Gantt Component
3 Jan 202424 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.
<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>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
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 chart 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.
<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>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
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.
<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>
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; }
}
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.
<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>
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.
<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>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Styling alternate rows
You can change the background colour of alternative rows in Gantt chart, by overriding the class as shown below.
.e-altrow, tr.e-chart-row:nth-child(even) {
background-color: #f2f2f2;
}
<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>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
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.
<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>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Customize rows and cells
While rendering the TreeGrid part in Gantt, the RowDataBound
and QueryCellInfo
events trigger for every row and cell. Using these events, you can customize the rows and cells. The following code example shows how to customize the cell and row elements using these events.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" queryCellInfo="queryCellInfo" 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 queryCellInfo(args) {
if (args.column.field == "Progress") {
if (args.data.Progress < 60)
args.cell.style.backgroundColor = "lightgreen"
else
args.cell.style.backgroundColor = "yellow"
}
}
function rowDataBound(args) {
if (args.data.TaskId == 4)
args.row.style.backgroundColor = "red"
}
</script>
public IActionResult Index()
{
ViewBag.DataSource = ganttData();
return View();
}
public static List<GanttDataSource> ganttData()
{
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>(),
};
GanttDataSource Child1 = new GanttDataSource()
{
TaskId = 2,
TaskName = "Identify site location",
StartDate = new DateTime(2019, 04, 02),
Duration = 4,
Progress = 70,
};
GanttDataSource Child2 = new GanttDataSource()
{
TaskId = 3,
TaskName = "Perform soil test",
StartDate = new DateTime(2019, 04, 02),
Duration = 4,
Progress = 50
};
GanttDataSource Child3 = new GanttDataSource()
{
TaskId = 4,
TaskName = "Soil test approval",
StartDate = new DateTime(2019, 04, 02),
Duration = 4,
Progress = 50
};
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),
EndDate = new DateTime(2019, 04, 21),
SubTasks = new List<GanttDataSource>()
};
GanttDataSource Child4 = new GanttDataSource()
{
TaskId = 6,
TaskName = "Develop floor plan for estimation",
StartDate = new DateTime(2019, 04, 04),
Duration = 3,
Progress = 70
};
GanttDataSource Child5 = new GanttDataSource()
{
TaskId = 7,
TaskName = "List materials",
StartDate = new DateTime(2019, 04, 04),
Duration = 3,
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 List<GanttDataSource> SubTasks { get; set; }
}
Clip mode
The clip mode provides options to display its overflow cell content and it can be defined by the Columns.ClipMode
property.
The following are three types of ClipMode
:
-
Clip
: Truncates the cell content when it overflows its area. -
Ellipsis
: Displays ellipsis when content of the cell overflows its area. -
EllipsisWithTooltip
: Displays ellipsis when content of the cell overflows its area; it displays the tooltip content when hover over ellipsis.
NOTE
By default, all the column’s
ClipMode
property is defined asEllipsisWithTooltip
.
Cell tooltip
You can enable or disable the Grid cell tooltip using the Columns.ClipMode
property.
<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>
<e-gantt-columns>
<e-gantt-column field="TaskId" headerText="Task ID" textAlign="Right" width="50"></e-gantt-column>
<e-gantt-column field="TaskName" headerText="Task Name" clipMode="EllipsisWithTooltip" width="100"></e-gantt-column>
<e-gantt-column field="StartDate" headerText="Start Date"></e-gantt-column>
<e-gantt-column field="Duration" headerText="Duration" clipMode="Clip"></e-gantt-column>
<e-gantt-column field="Progress" headerText="Progress"></e-gantt-column>
</e-gantt-columns>
</ejs-gantt>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}