Data Source
- 4 minutes to read
The GanttControl.DataSource property allows you to specify the control’s data source.
Note that DevExpress data-aware controls are compatible with any data access technology (ADO.NET, Entity Framework, XPO, etc.) and capable to display data from any data source that implements the IList, IBindingList, or ITypedList interface. Refer to these help topics for more details: Traditional Data Binding Methods and Data Binding Common Concepts.
Field Names
The following properties specify data source fields that contain the data required by the control to display the project schedule (hierarchy, dates, dependencies, etc.):
Hierarchy
- GanttControl.TreeListMappings.KeyFieldName — Gets or sets the name of the key field that uniquely identifies records in the data source. This data source field must store unique values.
- GanttControl.TreeListMappings.ParentFieldName — Gets or sets the data source field that identifies each record’s parent.
Tasks
- GanttControl.ChartMappings.TextFieldName — Gets or sets the data source field (of the String type) that specifies the text displayed next to a task in the chart.
- GanttControl.ChartMappings.StartDateFieldName — Gets or sets the data source field (of the DateTime type) that specifies a task’s start date.
- GanttControl.ChartMappings.FinishDateFieldName — Gets or sets the data source field (of the DateTime type) that specifies a task’s finish date.
- GanttControl.ChartMappings.DurationFieldName — Gets or sets the data source field (of the TimeSpan type) that specifies a task’s duration (only work hours).
- GanttControl.ChartMappings.ProgressFieldName — Gets or sets the data source field (of the Single type) that specifies a task’s progress (from 0 to 100 percent).
Baselines
- GanttControl.ChartMappings.BaselineStartDateFieldName — Gets or sets the data source field (of the DateTime type) that specifies a task’s baseline start date. The baseline start field shows the planned beginning date for a task.
- GanttControl.ChartMappings.BaselineFinishDateFieldName — Gets or sets the data source field (of the DateTime type) that specifies a task’s baseline finish date. The baseline finish field shows the planned completion date for a task.
- GanttControl.ChartMappings.BaselineDurationFieldName — Gets or sets the data source field (of the TimeSpan type) that specifies a task’s baseline duration. The baseline duration field shows the original span of time planned to complete a task.
Baselines are only displayed if the GanttControl.OptionsView.ShowBaselines option is enabled.
Dependencies
- GanttControl.ChartMappings.PredecessorsFieldName — Gets or sets the data source field that specifies a task’s predecessors. The data field should contain an IList object or a String value with keys separated by space, comma, or semicolon (‘ ‘, ‘,’ ‘;’). This field contains ID keys for predecessor tasks on which the current task depends.
This field supports the start-to-finish dependency type only. To specify other dependency types, you should use a separate data source. See Task Dependencies for more information.
How to Create Data Source in Code
The code below shows how to initialize tree list and chart mappings.
ganttControl1.TreeListMappings.KeyFieldName = "ID";
ganttControl1.TreeListMappings.ParentFieldName = "ParentID";
ganttControl1.ChartMappings.TextFieldName = "Text";
ganttControl1.ChartMappings.StartDateFieldName = "StartDate";
ganttControl1.ChartMappings.FinishDateFieldName = "FinishDate";
ganttControl1.ChartMappings.BaselineStartDateFieldName = "BaselineStartDate";
ganttControl1.ChartMappings.BaselineFinishDateFieldName = "BaselineFinishDate";
ganttControl1.OptionsView.ShowBaselines = true;
ganttControl1.ChartMappings.PredecessorsFieldName = "Predecessors";
ganttControl1.DataSource = GetTasks();
DataTable GetTasks() {
DataTable table = new DataTable();
DataColumn id = new DataColumn("ID", typeof(int));
DataColumn parentId = new DataColumn("ParentID", typeof(int));
DataColumn text = new DataColumn("Text", typeof(string));
DataColumn start = new DataColumn("StartDate", typeof(DateTime));
DataColumn finish = new DataColumn("FinishDate", typeof(DateTime));
DataColumn startBaseline = new DataColumn("BaselineStartDate", typeof(DateTime));
DataColumn finishBaseline = new DataColumn("BaselineFinishDate", typeof(DateTime));
DataColumn predecessors = new DataColumn("Predecessors", typeof(string));
table.Columns.AddRange(new DataColumn[] { id, parentId, text, start, finish, startBaseline, finishBaseline, predecessors });
table.Rows.Add(new object[] { 1, 0, "Task 1", DateTime.Now, DateTime.Now.AddDays(1), DateTime.Now, DateTime.Now.AddDays(1.5), null });
table.Rows.Add(new object[] { 2, 0, "Task 2", DateTime.Now.AddDays(1), DateTime.Now.AddDays(2), DateTime.Now.AddDays(1), DateTime.Now.AddDays(1.5), 1 });
table.Rows.Add(new object[] { 3, 0, "Task 3", DateTime.Now.AddDays(2), DateTime.Now.AddDays(3), DateTime.Now.AddDays(2), DateTime.Now.AddDays(3), "1, 2" });
return table;
}
Tip
Run the Code Examples - Data-binding modes - Map data source fields to task and node properties module in the XtraGantt GanttDemo for another example. Click Run In Visual Studio for source codes.