Back to Devexpress

Step 2 – Populate the Control with Data

vcl-179007-expressganttcontrol-getting-started-step-2-populate-the-control-with-data.md

latest4.3 KB
Original Source

Step 2 – Populate the Control with Data

  • Nov 19, 2024
  • 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:

delphi
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;
cpp
TdxGanttControlTasks* ATaskCollection = dxGanttControl1->DataModel->Tasks; // Provides access to the task collection
  TDateTime ADate = EncodeDateTime(2020, 11, 19, 8, 0, 0, 0);
  for (int I = 1; I <= 10; I++)
  {
    TdxGanttControlTask* 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)
      ATask->PredecessorLinks->Append()->PredecessorUID = ATaskCollection->Items[I - 1]->UID; // Links a task to a predecessor
    ADate = IncDay(ADate);
// Checks if a task is planned on a workday
    while (!dxGanttControl1->DataModel->ActiveCalendar->IsWorkday(ADate))
      ADate = IncDay(ADate);
  }

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:

delphi
procedure MyForm.FormCreate(Sender: TObject);
begin
  dxGanttControl1.LoadFromFile('C:\Users\Public\Documents\DevExpress\VCL\Demos\ExpressGantt Control\Data\SoftDev.xml');
end;
cpp
void __fastcall MyForm::FormCreate(TObject *Sender)
{
dxGanttControl1->LoadFromFile("C:\Users\Public\Documents\DevExpress\VCL\Demos\ExpressGantt Control\Data\SoftDev.xml");
}

Chart XML File Export

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

delphi
begin
dxGanttControl1.SaveToFile('D:\MyGanttChart.xml');
end;
cpp
{
dxGanttControl1->SaveFile("D:\MyGanttChart.xml");
}

Build the application project to see the chart.

See Also

Step 1 – Add the Control to a Form

Step 3 – Adjust the Control

VCL Gantt Control – Getting Started