Back to Devexpress

Task Progress

windowsforms-401787-controls-and-libraries-gantt-control-task-progress.md

latest6.7 KB
Original Source

Task Progress

  • Mar 16, 2021
  • 4 minutes to read

A task’s progress is a value that indicates the percentage of work completed. In the chart, a task displays the completed and upcoming work in different colors.

Bind to Data

Use the ProgressFieldName property to specify the data source field that stores the progress. The field’s data type should be Single and contain values from 0 to 100.

csharp
ganttControl1.ChartMappings.ProgressFieldName = "Progress";
vb
ganttControl1.ChartMappings.ProgressFieldName = "Progress"

See the following topic for more information on how to bind the control to a data source: Bind to Data Source.

Edit Task Progress

Users can edit a task’s progress in the task list or in the chart. You can edit progress in code.

Edits in Task List

The control generates columns in the task list for all fields in the bound data source. A user can use a column’s editor to update a cell value.

The control uses a TextEdit to edit cell values. You can substitute it with a custom editor. The code below shows how to use a SpinEdit with a custom input mask to display and edit the progress.

csharp
RepositoryItemSpinEdit editor = new RepositoryItemSpinEdit();
editor.Mask.EditMask = "P";
editor.Mask.UseMaskAsDisplayFormat = true;
progressColumn.ColumnEdit = editor;
vb
Dim editor As New RepositoryItemSpinEdit()
editor.Mask.EditMask = "P"
editor.Mask.UseMaskAsDisplayFormat = True
progressColumn.ColumnEdit = editor

Edits in Chart

Enable the AllowModifyProgress option to allow users to modify the progress in the chart. You can also hide the progress column in the task list.

csharp
progressColumn.Visible = false;
ganttControl1.OptionsCustomization.AllowModifyProgress = DefaultBoolean.True;
vb
progressColumn.Visible = False
ganttControl1.OptionsCustomization.AllowModifyProgress = DefaultBoolean.True

See the following topic for more information: Interactive Editing.

Edits in Code

Use a GanttControlNode object’s indexer to access the progress column in code and specify its value. To obtain the progress in code, you can also call a node’s GetProgress() method.

csharp
// Get the value.
float progress = (float)node.GetProgress();
float progress1 = (float)node.GetValue(progressColumn);
float progress2 = (float)node["Progress"];
// Set the value.
node.SetValue(progressColumn, 100);
node["Progress"] = 100;
vb
Dim node As New GanttControlNode()
' Get the value.
Dim progress As Single = CSng(node.GetProgress())
Dim progress1 As Single = CSng(node.GetValue(progressColumn))
Dim progress2 As Single = CSng(node("Progress"))
' Set the value.
node.SetValue(progressColumn, 100)
node("Progress") = 100

Automatically Recalculate Task Progress

If the UpdateDependentTaskProgress option is enabled, the control automatically recalculates the progress when changes are made. That is, when a user modifies a particular task’s progress, the control updates the progress in all dependent tasks:

  • for a regular task — the control recalculates the progress for all summary tasks (the total progress).

  • for a summary task (the total progress) — the control recalculates the progress for all sub-tasks and parent summary tasks.

If the UpdateDependentTaskProgress option is disabled or if you updated the progress in the data source, the control does not update the progress in dependent tasks. You should call the UpdateSummaryProgress method to update the progress for all summary tasks in a tree branch that starts from the specified task. If the summary task is not specified, the method updates all tasks in the project.

Adjust Recalculated Task Progress

The CustomTaskProgress event fires when the control recalculated a dependent task’s progress. You can handle this event to specify a custom value.

The code below handles the CustomTaskProgress event to apply a coefficient to the contribution of a particular task to the total progress.

csharp
ganttControl1.CustomTaskProgress += GanttControl1_CustomTaskProgress;

private void GanttControl1_CustomTaskProgress(object sender, DevExpress.XtraGantt.CustomTaskProgressEventArgs e) {
    if(e.Node.HasChildren) {
        float result = 0;
        foreach(GanttControlNode node in e.Node.Nodes)
            result += (float)node["Progress"] / 100 * (float)node["ProgressCoefficient"];
        e.Progress = result;
    }
}
vb
AddHandler ganttControl1.CustomTaskProgress, AddressOf GanttControl1_CustomTaskProgress

Private Sub GanttControl1_CustomTaskProgress(ByVal sender As Object, ByVal e As DevExpress.XtraGantt.CustomTaskProgressEventArgs)
    If e.Node.HasChildren Then
        Dim result As Single = 0
        For Each node As GanttControlNode In e.Node.Nodes
            result += DirectCast(node("Progress"), Single) / 100 * DirectCast(node("ProgressCoefficient"), Single)
        Next node
        e.Progress = result
    End If
End Sub