windowsforms-401290-controls-and-libraries-gantt-control-tasks.md
The GanttControlNode class represents a node in the task list. The control automatically determines a task’s type based on its duration and the presence of subtasks. The following task types are supported:
Task — a regular task that has a certain duration and does not have subtasks.
Summary task — a task that has subtasks. These tasks show combined information for their subtasks.
Milestone — a task with a zero duration. These tasks show important dates in the project.
The control also displays dependencies between tasks. A task can have a predecessor task that should be accomplished before the task can start. The figure below illustrates different task types and dependencies between them.
A task can have captions displayed inside, to the left, and to the right of the bar. Handle the GanttControl.CustomTaskDisplayText event to show captions.
The code below shows how to display captions to the left and to the right of the bars.
HashSet<int> criticalPathIds = new HashSet<int> { 1, 2, 3, 6, 7, 8, 10, 11, 13 };
ganttControl.CustomTaskDisplayText += (sender, e) => {
int taskId = Convert.ToInt32(e.Node.GetValue("Id"));
if(criticalPathIds.Contains(taskId)) {
e.RightText = "High priority";
}
else {
e.RightText = string.Empty;
e.LeftText = "Normal priority";
}
};
Dim criticalPathIds As New HashSet(Of Integer)() From {1, 2, 3, 6, 7, 8, 10, 11, 13}
AddHandler ganttControl.CustomTaskDisplayText, Sub(sender, e)
Dim taskId As Integer = Convert.ToInt32(e.Node.GetValue("Id"))
If criticalPathIds.Contains(taskId) Then
e.RightText = "High priority"
Else
e.RightText = String.Empty
e.LeftText = "Normal priority"
End If
End Sub
Note
Run the Gantt Code Examples demo to see the complete example.
When a user right-clicks a task’s bar, the control can show a context menu. To populate the menu with commands, handle the GanttControl.TaskPopupMenuShowing event.
The code below shows how to populate the context menu with items.
using DevExpress.XtraEditors;
using DevExpress.XtraGantt;
private void ganttControl1_TaskPopupMenuShowing(object sender, GanttTaskPopupMenuShowingEventArgs e) {
e.Items.Add(new DevExpress.Utils.Menu.DXMenuItem("Show Text", (ss, ee) => { XtraMessageBox.Show(e.Node.GetText()); }));
}
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGantt
Private Sub ganttControl1_TaskPopupMenuShowing(ByVal sender As Object, ByVal e As GanttTaskPopupMenuShowingEventArgs) _
Handles ganttControl1.TaskPopupMenuShowing
e.Items.Add(New DevExpress.Utils.Menu.DXMenuItem("Show Text", Sub(ss, ee)
XtraMessageBox.Show(e.Node.GetText())
End Sub))
End Sub
To show a tooltip for a task, handle GanttControl.TaskToolTipShowing event.
The code below shows a regular tooltip for a task.
using DevExpress.XtraGantt;
private void ganttControl1_TaskToolTipShowing(object sender, GanttTaskToolTipShowingEventArgs e) {
e.Text = e.Node.GetText();
}
Imports DevExpress.XtraGantt
Private Sub ganttControl1_TaskToolTipShowing(ByVal sender As Object, ByVal e As GanttTaskToolTipShowingEventArgs) _
Handles ganttControl1.TaskToolTipShowing
e.Text = e.Node.GetText()
End Sub
You can handle the following events to draw a task manually:
GanttControl.CustomDrawTask — allows you to draw a bar that represents a task in the chart area.
GanttControl.CustomDrawTaskDependency — allows you to draw a dependency line.
The code below shows how to highlight specific tasks and dependencies.
Run Demo: Highlight Tasks and Dependencies
HashSet<int> tasks = new HashSet<int> { 1, 2, 3, 6, 7, 8, 10, 11, 13 };
ganttControl.CustomDrawTask += (sender, e) => {
int taskId = Convert.ToInt32(e.Node.GetValue("Id"));
if(tasks.Contains(taskId)) {
e.Appearance.BackColor = DXSkinColors.FillColors.Warning;
e.Appearance.ProgressColor = DXSkinColors.FillColors.Warning;
}
};
ganttControl.CustomDrawTaskDependency += (sender, e) => {
int predecessorId = Convert.ToInt32(e.PredecessorNode.GetValue("Id"));
int successorId = Convert.ToInt32(e.SuccessorNode.GetValue("Id"));
if(tasks.Contains(predecessorId) && tasks.Contains(successorId)) {
e.Appearance.BackColor = DXSkinColors.FillColors.Warning;
}
};
Dim tasks As New HashSet(Of Integer)() From {1, 2, 3, 6, 7, 8, 10, 11, 13}
AddHandler ganttControl.CustomDrawTask, Sub(sender, e)
Dim taskId As Integer = Convert.ToInt32(e.Node.GetValue("Id"))
If tasks.Contains(taskId) Then
e.Appearance.BackColor = DXSkinColors.FillColors.Warning
e.Appearance.ProgressColor = DXSkinColors.FillColors.Warning
End If
End Sub
AddHandler ganttControl.CustomDrawTaskDependency, Sub(sender, e)
Dim predecessorId As Integer = Convert.ToInt32(e.PredecessorNode.GetValue("Id"))
Dim successorId As Integer = Convert.ToInt32(e.SuccessorNode.GetValue("Id"))
If tasks.Contains(predecessorId) AndAlso tasks.Contains(successorId) Then
e.Appearance.BackColor = DXSkinColors.FillColors.Warning
End If
End Sub