vcl-179007-expressganttcontrol-getting-started-step-2-populate-the-control-with-data.md
You can create charts in code , import them from XML files and save created charts.
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;
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.
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;
void __fastcall MyForm::FormCreate(TObject *Sender)
{
dxGanttControl1->LoadFromFile("C:\Users\Public\Documents\DevExpress\VCL\Demos\ExpressGantt Control\Data\SoftDev.xml");
}
Call the SaveToFile procedure to save a chart to an XML file:
begin
dxGanttControl1.SaveToFile('D:\MyGanttChart.xml');
end;
{
dxGanttControl1->SaveFile("D:\MyGanttChart.xml");
}
Build the application project to see the chart.
See Also