Columns in in gantt control
3 Jan 202424 minutes to read
The column displays information from a bound data source, and you can edit the values of column to update the task details through TreeGrid. The operations such as sorting, filtering, and searching can be performed based on column definitions. To display a Gantt column, the Field
property should be mapped from the data source to the column.
NOTE
If the column
Field
is not specified in the data source, the column values will be empty.
The TreeColumnIndex
property is used to define the expander column in the Gantt control to expand and collapse the child rows.
Defining columns
Using the Columns
property, you can define the columns in Gantt. If the columns are not defined, then the default columns will be rendered based on the mapped data source fields in the TaskFields
property. Refer to the following code example for defining the columns in Gantt along with their widths.
<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" width="150"> </e-gantt-column>
<e-gantt-column field="TaskName" headerText="Job Name" width="250"></e-gantt-column>
</e-gantt-columns>
</ejs-gantt>
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 = 50
};
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 = 50
};
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; }
}
Custom column header
The column header text can be defined using the HeaderText
property, and you can customize the column headers using the HeaderTemplate
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="TaskName" headerTemplate="#projectName" width="150"></e-gantt-column>
<e-gantt-column field="StartDate" headerTemplate="#dateTemplate" width="150"></e-gantt-column>
<e-gantt-column field="Duration" headerTemplate="#durationTemplate" width="150"></e-gantt-column>
<e-gantt-column field="Progress" headerTemplate="#progressTemplate" width="150"></e-gantt-column>
</e-gantt-columns>
</ejs-gantt>
<script type="text/x-template" id="projectName">
<div>
<div>
<img src="taskname.png" width="20" height="20" class="e-image" /> Task Name
</div>
</div>
</script>
<script type="text/x-template" id="dateTemplate">
<div>
<div>
<img src="startdate.png" width="20" height="20" class="e-image" /> Start Date
</div>
</div>
</script>
<script type="text/x-template" id="durationTemplate">
<div>
<div>
<img src="duration.png" width="20" height="20" class="e-image" /> Duration
</div>
</div>
</script>
<script type="text/x-template" id="progressTemplate">
<div>
<div>
<img src="progress.png" width="20" height="20" class="e-image" /> Progress
</div>
</div>
</script>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Format
To format the cell values based on a specific culture, use the Columns.Format
property. The Gantt control uses the Internationalization
library to format number
and date
values.
<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" width="150"></e-gantt-column>
<e-gantt-column field="Progress" width="150" format="C"></e-gantt-column>
</e-gantt-columns>
</ejs-gantt>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
NOTE
By default, the
number
anddate
values are formatted inen-US
culture.
Number formatting
The number or integer values can be formatted using the following format strings.
Format | Description | Remarks |
---|---|---|
N | Denotes numeric type. | The numeric format is followed by an integer value like N2 or N3, which denotes the number of precisions to be allowed. |
C | Denotes currency type. | The currency format is followed by an integer value like C2 or C3, which denotes the number of precisions to be allowed. |
P | Denotes percentage type | The percentage format expects the input value to be in the range of 0 to 100. For example, the cell value 0.2 is formatted as 20% . The percentage format is followed by an integer value like P2, P3, which denotes the number of precisions to be allowed. |
Date formatting
You can format date values either using the built-in date format string or a custom format string.
For the built-in date format, you can specify the Columns.Format
property as string (example: yMd
).
You can also use the custom format string to format the date values. Some of the custom formats and the formatted date values are given in the following table.
Format | Formatted value |
---|---|
{ type:’date’, format:’dd/MM/yyyy’ } | 04/07/2019 |
{ type:’date’, format:’dd.MM.yyyy’ } | 04.07.2019 |
{ type:’date’, skeleton:’short’ } | 7/4/19 |
{ type: ‘dateTime’, format: ‘dd/MM/yyyy hh:mm a’ } | 04/07/2019 12:00 AM |
{ type: ‘dateTime’, format: ‘MM/dd/yyyy hh:mm:ss a’ } | 07/04/2019 12:00:00 AM |
<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" width="50"></e-gantt-column>
<e-gantt-column field="TaskName"></e-gantt-column>
<e-gantt-column field="StartDate" format="yMd"></e-gantt-column>
<e-gantt-column field="Duration"></e-gantt-column>
<e-gantt-column field="Progress"></e-gantt-column>
</e-gantt-columns>
</ejs-gantt>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Change tree/expander column
The tree/expander column is a column in the Gantt control, that has icons to expand or collapse the parent records. You can define the tree column index in the Gantt control by using the TreeColumnIndex
property and the default value of this property is 0
. The following code example shows how to use this property.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" treeColumnIndex="2">
<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();
}
Show or hide columns dynamically
You can show or hide gantt columns dynamically using external buttons by invoking the showColumn
or hideColumn
method. The Progress column is hidden and shown on button clicking.
<ejs-button id="show" cssClass="e-flat" content="Show Columns"></ejs-button>
<ejs-button id="hide" cssClass="e-flat" content="Hide Columns"></ejs-button>
<ejs-gantt id='Gantt' dataSource="ViewBag.DataSource" height="550px" gridLines="Both" treeColumnIndex="1">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" dependency="Predecessor" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-columns>
<e-gantt-column field="TaskId"></e-gantt-column>
<e-gantt-column field="TaskName" headerText="Name" width="250"></e-gantt-column>
<e-gantt-column field="Progress"></e-gantt-column>
<e-gantt-column field="StartDate"></e-gantt-column>
<e-gantt-column field="Duration"></e-gantt-column>
</e-gantt-columns>
</ejs-gantt>
<script>
document.getElementById("show").addEventListener("click", function () {
var gantt = document.getElementById("Gantt").ej2_instances[0];
gantt.showColumn(['Duration']); //show by HeaderText
});
document.getElementById("hide").addEventListener("click", function () {
var gantt = document.getElementById("Gantt").ej2_instances[0];
gantt.hideColumn(['Duration']); //hide by HeaderText
})
</script>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Controlling gantt column actions
You can enable or disable gantt action for a particular column by setting the allowFiltering
, allowSorting
, allowReordering
, and allowEditing
properties.
<ejs-gantt id='Gantt' dataSource="ViewBag.DataSource" height="550px" gridLines="Both" treeColumnIndex="1"
allowSorting="true" allowReordering="true" allowFiltering="true">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" dependency="Predecessor" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-columns>
<e-gantt-column field="TaskId"></e-gantt-column>
<e-gantt-column field="TaskName" headerText="Name" allowSorting="false" width="250"></e-gantt-column>
<e-gantt-column field="Progress" allowReordering="false"></e-gantt-column>
<e-gantt-column field="StartDate" allowEditing="false"></e-gantt-column>
<e-gantt-column field="Duration" allowFiltering="false"></e-gantt-column>
</e-gantt-columns>
<e-gantt-editsettings allowEditing="true"></e-gantt-editsettings>
</ejs-gantt>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Column type
Column type can be specified using the columns.type
property. It specifies the type of data the column binds.
If the format
is defined for a column, the column uses type
to select the appropriate format option number or date.
Gantt column supports the following types:
- string
- number
- boolean
- date
- date-time
NOTE
If the
type
is not defined, it will be determined from the first record of thedataSource
. In case if the first record of thedataSource
is null/blank value for a column then it is necessary to define thetype
for that column.