Blazorise Gantt: Data Binding
Bind Gantt to your own task model through configurable field mappings.
Data Model
Gantt<TItem> binds to your own model type through field mapping parameters such as IdField, StartField, and EndField. You can keep your existing model names and map them without creating a dedicated DTO.
Must-Have Fields
For most production scenarios, these are the fields you should map:
StartFieldandEndField: Required to render timeline bars. If either value is missing/unassigned, the item cannot be drawn on the timeline.TitleField: Recommended for readable tree rows and task labels.IdField: Strongly recommended for stable identity, selection/state behavior, and CRUD operations.DurationField(optional): Used for duration column/editing. If not mapped, duration is calculated fromStartFieldandEndField.ProgressField(optional): Enables progress percentage in bars, columns, and the edit dialog slider.
Flat vs Hierarchical Data
Gantt supports both data shapes:
- Flat data: One collection with
IdFieldandParentIdField. This is usually best for API-driven data. - Hierarchical data: Nested child collections mapped through
ItemsField. -
If both
ParentIdFieldandItemsFieldexist, Gantt uses flat mode by default. SetHierarchicalData="true"to force hierarchical mode. -
If
ItemsFieldexists andParentIdFielddoes not, hierarchical mode is used automatically.
Flat Data Binding
Use the classicId + ParentId model when your API already returns a flat task list.
<Gantt TItem="TaskItem" Data="" Date="" SelectedView="GanttView.Week" DurationField="Duration"> <GanttColumns> <GanttColumn Field="Title" Title="Task" Expandable Width="Width.Px(220)" /> <GanttColumn Field="Start" Width="Width.Px(130)" /> <GanttColumn Field="End" Width="Width.Px(130)" Visible="false" /> <GanttColumn Field="Duration" Width="Width.Px(90)" TextAlignment="TextAlignment.Center" Visible="false" /> </GanttColumns> <GanttToolbar /> <GanttViews> <GanttWeekView TimelineCellWidth="90" RowHeight="44" /> </GanttViews> </Gantt>
@code { private DateOnly selectedDate = DateOnly.FromDateTime( DateTime.Today ); private List<TaskItem> tasks = CreateTasks(); private static List<TaskItem> CreateTasks() { var baseDate = DateTime.Today.AddDays( -2 ); var result = new List<TaskItem> { new() { Id = "1", Title = "Website redesign", Start = baseDate, End = baseDate.AddDays( 16 ) }, new() { Id = "2", ParentId = "1", Title = "Discovery", Start = baseDate, End = baseDate.AddDays( 4 ) }, new() { Id = "3", ParentId = "1", Title = "Implementation", Start = baseDate.AddDays( 4 ), End = baseDate.AddDays( 14 ) }, new() { Id = "4", ParentId = "3", Title = "Components", Start = baseDate.AddDays( 5 ), End = baseDate.AddDays( 10 ) }, new() { Id = "5", ParentId = "3", Title = "Accessibility pass", Start = baseDate.AddDays( 10 ), End = baseDate.AddDays( 13 ) }, new() { Id = "6", ParentId = "1", Title = "Launch", Start = baseDate.AddDays( 14 ), End = baseDate.AddDays( 16 ) }, }; foreach ( var item in result ) { item.Duration = Math.Max( 1, (int)Math.Ceiling( ( item.End - item.Start ).TotalDays ) ); } return result; } public class TaskItem { public string Id { get; set; } public string ParentId { get; set; } public string Title { get; set; } public DateTime Start { get; set; } public DateTime End { get; set; } public int Duration { get; set; } } }
Hierarchical Data Binding
Bind directly to nested task collections withItemsField for tree-shaped data models.
<Gantt TItem="TaskItem" Data="" Date="" SelectedView="GanttView.Week" ItemsField="Children" HierarchicalData DurationField="Duration"> <GanttColumns> <GanttColumn Field="Wbs" Width="Width.Px(76)" TextAlignment="TextAlignment.Center" Visible="false" /> <GanttColumn Field="Title" Title="Task" Expandable Width="Width.Px(220)" /> <GanttColumn Field="Start" Width="Width.Px(130)" /> <GanttColumn Field="End" Width="Width.Px(130)" Visible="false" /> <GanttColumn Field="Duration" Width="Width.Px(90)" TextAlignment="TextAlignment.Center" Visible="false" /> </GanttColumns> <GanttToolbar /> <GanttViews> <GanttWeekView TimelineCellWidth="90" RowHeight="44" /> </GanttViews> </Gantt>
@code { private DateOnly selectedDate = DateOnly.FromDateTime( DateTime.Today ); private List<TaskItem> tasks = CreateTasks(); private static List<TaskItem> CreateTasks() { var baseDate = DateTime.Today.AddDays( -2 ); var result = new List<TaskItem> { new() { Id = "1", Title = "Mobile app release", Start = baseDate, End = baseDate.AddDays( 18 ), Children = new() { new() { Id = "2", Title = "Planning", Start = baseDate, End = baseDate.AddDays( 4 ), }, new() { Id = "3", Title = "Execution", Start = baseDate.AddDays( 4 ), End = baseDate.AddDays( 16 ), Children = new() { new() { Id = "4", Title = "API integration", Start = baseDate.AddDays( 5 ), End = baseDate.AddDays( 11 ), }, new() { Id = "5", Title = "QA", Start = baseDate.AddDays( 11 ), End = baseDate.AddDays( 16 ), }, }, }, new() { Id = "6", Title = "Go-live", Start = baseDate.AddDays( 16 ), End = baseDate.AddDays( 18 ), }, }, }, }; UpdateDuration( result ); return result; } private static void UpdateDuration( IEnumerable<TaskItem> items ) { foreach ( var item in items ) { item.Duration = Math.Max( 1, (int)Math.Ceiling( ( item.End - item.Start ).TotalDays ) ); UpdateDuration( item.Children ); } } public class TaskItem { public string Id { get; set; } public string Title { get; set; } public DateTime Start { get; set; } public DateTime End { get; set; } public int Duration { get; set; } public List<TaskItem> Children { get; set; } = new(); } }
API
See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.