Skip to main content

Step 2 – Populate the Control with Data

  • 3 minutes to read

You can create charts in code, import them from XML files and save created charts.

Create Tasks in Code

The code examples below populate the control with tasks linked with the “Finish-To-Start” relationships:

var
  ADate: TDateTime;
  ATask: TdxGanttControlTask;
  ATaskCollection: TdxGanttControlTasks;
  I: Integer;
begin
  ATaskCollection := dxGanttControl1.DataModel.Tasks;  // Provides access to the task collection
  ADate := EncodeDateTime(2020, 11, 19, 8, 0, 0, 0);
  for I := 1 to 10 do
  begin
    ATask := ATaskCollection.Append;  // Appends a task to the collection
    ATask.Manual := False;  // Specifies that a task is automatically scheduled
    ATask.OutlineLevel := 1;  // Specifies a task's nesting level
    ATask.Start := ADate;  // Assigns a start date to a task
    ATask.Finish := ADate + EncodeTime(9, 0, 0, 0);  // Assigns a finish date to a task
    ATask.Name := 'Task #' + IntToStr(I);  // Specifies a task name
    if I > 1 then
      ATask.PredecessorLinks.Append.PredecessorUID := ATaskCollection[ATaskCollection.Count - 2].UID;  // Links a task to a predecessor
    ADate := IncDay(ADate);
// Checks if a task is planned on a workday
    while not dxGanttControl1.DataModel.ActiveCalendar.IsWorkday(ADate) do
      ADate := IncDay(ADate);
  end;

Build the application project to see the chart.

Chart XML File Import

The control supports Gantt chart import. You can work with charts created, for instance, in Microsoft Project. Save the chart as an XML file as shown below:

Handle the form’s OnCreate event and load the Gantt chart from the “SoftDev” XML file shipped with the control’s demo:

procedure MyForm.FormCreate(Sender: TObject);
begin
  dxGanttControl1.LoadFromFile('C:\Users\Public\Documents\DevExpress\VCL\Demos\ExpressGantt Control\Data\SoftDev.xml');
end;

Chart XML File Export

Call the SaveToFile procedure to save a chart to an XML file:

begin
dxGanttControl1.SaveToFile('D:\MyGanttChart.xml');
end;

Build the application project to see the chart.

See Also