How to use gantt with observable collection ?

Hi,

I just modified the defualt gannt code by using an observable. However adding tasks do not show up in the chart. I also tried to call refresh, ti does not work either.

See code below:

@using Syncfusion.Blazor.Gantt
@using Syncfusion.Blazor.Data
@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor
@using Syncfusion.Blazor.Popups
@using System.Collections.ObjectModel;


@page "/Foo"

<button @onclick="Add">Add</button><span>@TaskCollection?.Count</span>
<div class="col-lg-12 control-section">
<div class="content-wrapper">
<div class="row">
<SfGantt @ref="Gantt" DataSource="@TaskCollection" Height="450px" Width="100%" HighlightWeekends="true"
ProjectStartDate="@ProjectStart" ProjectEndDate="@ProjectEnd">
<GanttTaskFields Id="TaskId" Name="TaskName" StartDate="StartDate" EndDate="EndDate" Duration="Duration" Progress="Progress"
Dependency="Predecessor" ParentID="ParentId"></GanttTaskFields>
<GanttLabelSettings LeftLabel="TaskName" TValue="TaskData"> </GanttLabelSettings>
<GanttSplitterSettings Position="30%"> </GanttSplitterSettings>
</SfGantt>
</div>
</div>
</div>
@code{
public DateTime ProjectStart = new DateTime(2019, 3, 24);
public DateTime ProjectEnd = new DateTime(2019, 7, 6);
public ObservableCollection<TaskData> TaskCollection { get; set; }

public SfGantt<TaskData> Gantt { get; set; }

async Task Add()
{
var taskData = new TaskData()
{
TaskId = TaskCollection.Count + 1,
TaskName = "Added:" + TaskCollection.Count + 1,
StartDate = new DateTime(2019, 04, 02),
EndDate = new DateTime(2019, 04, 08),
Duration = "5days"
};
TaskCollection.Add(taskData);
await Gantt.Refresh();
}
protected override void OnInitialized()
{
this.TaskCollection = new ObservableCollection<TaskData>()
{
new TaskData() {
TaskId = 1,
TaskName = "Product concept ",
StartDate = new DateTime(2019, 04, 02),
EndDate = new DateTime(2019, 04, 08),
Duration = "5days"
},
new TaskData() {
TaskId = 2,
TaskName = "Defining the product usage",
StartDate = new DateTime(2019, 04, 02),
EndDate = new DateTime(2019, 04, 08),
Duration = "3",
Progress = 30,
ParentId = 1,
BaselineStartDate = new DateTime(2019, 04, 02),
BaselineEndDate = new DateTime(2019, 04, 02),
}
};
}

public class TaskData
{
public int TaskId { get; set; }
public string TaskName { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public DateTime? BaselineStartDate { get; set; }
public DateTime? BaselineEndDate { get; set; }
public string Duration { get; set; }
public int Progress { get; set; }
public string Predecessor { get; set; }
public object ResourceId { get; set; }
public string Notes { get; set; }
public string TaskType { get; set; }
public int? ParentId { get; set; }
}
}

4 Replies

MS Monisha Sivanthilingam Syncfusion Team June 18, 2021 12:57 PM UTC

Hi Joe, 
 
Greetings from Syncfusion support. 
 
We have analyzed your query and would like to suggest the AddRecord public method available in Gantt Chart to add a record to your Gantt Chart. The following code snippets demonstrate the solution. 
 
Index.razor 
 
public void Add() 
{ 
   var taskData = new TaskData() 
   { 
            TaskId = Convert.ToInt32(TaskCollection.Count) + 1, 
            TaskName = "Added:" + (Convert.ToInt32(TaskCollection.Count) + 1), 
            StartDate = new DateTime(2019, 04, 02), 
            EndDate = new DateTime(2019, 04, 08), 
            Duration = "5" 
   }; 
  this.Gantt.AddRecord(taskData, 1, RowPosition.Below); 
} 
 
 
We have prepared a sample  for your reference. 
 
Please contact us if you require any further assistance. 
 
Regards, 
Monisha. 



JR Joe Robe June 18, 2021 03:02 PM UTC

Thanks, but it would be great if you actually would try you example :)

At first add , the count of the collection does not increase.
If you press add the second time, all data on the left side disappears and now the count goes up from 2 to 4 !
Clicking again nothing happens but count goes up to 6 ! More clicking does NOTHING

If add Gantt.Refresh() after ther add, the left data immediately disappers.






MS Monisha Sivanthilingam Syncfusion Team June 21, 2021 01:51 PM UTC

Hi Joe, 
 
We were able to replicate the issue you reported. We will validate it and provide you with further details tomorrow(June 22,2021). 
 
We appreciate your patience until then. 
 
Regards, 
Monisha. 



MS Monisha Sivanthilingam Syncfusion Team June 22, 2021 12:25 PM UTC

Hi Joe, 
 
Thank you for your patience. 
 
We have confirmed the issue with adding new records through the AddRecord method as a bug and have logged a bug report for it. You can track its status from the below feedback link. 
 
 
The fix will be provided in the patch release scheduled for the second week of July, 2021. 
 
Regards, 
Monisha. 


Loader.
Up arrow icon