|
| 1 | +--- |
| 2 | +title: DataBound Column |
| 3 | +page_title: Gantt Tree - DataBound Column |
| 4 | +description: Data binding and bound column properties in Gantt for Blazor. |
| 5 | +slug: gantt-columns-bound |
| 6 | +tags: telerik,blazor,gantt,bound,column |
| 7 | +published: True |
| 8 | +position: 0 |
| 9 | +--- |
| 10 | + |
| 11 | +# Gantt DataBound Column |
| 12 | + |
| 13 | +This article explains the basics of showing data in a Gantt Tree and the features of its bound columns. |
| 14 | + |
| 15 | +Sections in this article: |
| 16 | + |
| 17 | +* [Show Data In A Gantt](#show-data-in-a-gantt) |
| 18 | +* [Gantt Bound Column Parameters](#gantt-bound-column-parameters) |
| 19 | +* [Notes](#notes) |
| 20 | + |
| 21 | + |
| 22 | +## Show Data In A Gantt |
| 23 | + |
| 24 | +To show data in a Gantt Chart, you must define `GanttColumn` instances in the `GanttColumns` collection for the fields of the data source you want to show. Their `Field` parameter defines which property from the model is shown in the column. You can provide the collection of models to the Gantt in its `Data` parameter. |
| 25 | + |
| 26 | +Since the Gantt is designed for hierarchical data, you should also define which column will hold the expand/collapse arrow for the child items. It can be any column, not necessarily the first, and you denote it by setting its `Expandable` parameter to `true`. |
| 27 | + |
| 28 | +You can read more details on how to tie the Gantt to your data fields and child elements in the [Data Binding Overview]({%slug gantt-data-binding-overview%}) article. It provides information on the features of the model, and describing the parent-child relationships in two different ways. |
| 29 | + |
| 30 | +>caption Provide data to the Gantt and choose which columns (fields) to see |
| 31 | +
|
| 32 | +````CSHTML |
| 33 | +@* define data, model and columns for a Gantt chart *@ |
| 34 | +
|
| 35 | +<TelerikGantt Data="@Data" |
| 36 | + Width="50%" |
| 37 | + Height="600px" |
| 38 | + IdField="Id" |
| 39 | + ParentIdField="ParentId"> |
| 40 | + <GanttViews> |
| 41 | + <GanttWeekView></GanttWeekView> |
| 42 | + </GanttViews> |
| 43 | + <GanttColumns> |
| 44 | + <GanttColumn Field="Id" |
| 45 | + Visible="false"> |
| 46 | + </GanttColumn> |
| 47 | + <GanttColumn Field="Title" |
| 48 | + Expandable="true" |
| 49 | + Width="160px" |
| 50 | + Title="Task Title" > |
| 51 | + </GanttColumn> |
| 52 | + <GanttColumn Field="PercentComplete" |
| 53 | + Width="60px"> |
| 54 | + </GanttColumn> |
| 55 | + <GanttColumn Field="Start" |
| 56 | + Width="100px" |
| 57 | + TextAlign="@ColumnTextAlign.Right"> |
| 58 | + </GanttColumn> |
| 59 | + <GanttColumn Field="End" |
| 60 | + DisplayFormat="End: {0:d}" |
| 61 | + Width="100px"> |
| 62 | + </GanttColumn> |
| 63 | + </GanttColumns> |
| 64 | +</TelerikGantt> |
| 65 | +
|
| 66 | +@code { |
| 67 | + public DateTime SelectedDate { get; set; } = new DateTime(2019, 11, 11, 6, 0, 0); |
| 68 | +
|
| 69 | + class FlatModel |
| 70 | + { |
| 71 | + public int Id { get; set; } |
| 72 | + public int? ParentId { get; set; } |
| 73 | + public string Title { get; set; } |
| 74 | + public double PercentComplete { get; set; } |
| 75 | + public DateTime Start { get; set; } |
| 76 | + public DateTime End { get; set; } |
| 77 | + } |
| 78 | +
|
| 79 | + public int LastId { get; set; } = 1; |
| 80 | + List<FlatModel> Data { get; set; } |
| 81 | +
|
| 82 | + protected override void OnInitialized() |
| 83 | + { |
| 84 | + Data = new List<FlatModel>(); |
| 85 | + var random = new Random(); |
| 86 | +
|
| 87 | + for (int i = 1; i < 6; i++) |
| 88 | + { |
| 89 | + var newItem = new FlatModel() |
| 90 | + { |
| 91 | + Id = LastId, |
| 92 | + Title = "Employee " + i.ToString(), |
| 93 | + Start = new DateTime(2020, 12, 6 + i), |
| 94 | + End = new DateTime(2020, 12, 11 + i), |
| 95 | + PercentComplete = Math.Round(random.NextDouble(), 2) |
| 96 | + }; |
| 97 | +
|
| 98 | + Data.Add(newItem); |
| 99 | + var parentId = LastId; |
| 100 | + LastId++; |
| 101 | +
|
| 102 | + for (int j = 0; j < 5; j++) |
| 103 | + { |
| 104 | + Data.Add(new FlatModel() |
| 105 | + { |
| 106 | + Id = LastId, |
| 107 | + ParentId = parentId, |
| 108 | + Title = " Employee " + i + " : " + j.ToString(), |
| 109 | + Start = new DateTime(2020, 12, 6 + i + j), |
| 110 | + End = new DateTime(2020, 12, 7 + i + j), |
| 111 | + PercentComplete = Math.Round(random.NextDouble(), 2) |
| 112 | + }); |
| 113 | +
|
| 114 | + LastId++; |
| 115 | + } |
| 116 | + } |
| 117 | +
|
| 118 | + base.OnInitialized(); |
| 119 | + } |
| 120 | +} |
| 121 | +```` |
| 122 | + |
| 123 | +>tip You can also use a string for the field name, using the `nameof` operator is not necessary.The field name is, however, **case-sensitive**. |
| 124 | +
|
| 125 | +>tip The `Data` collection can be an `ObservableCollection`, an array, a `List` - it must only implement `IEnumerable`. |
| 126 | +
|
| 127 | + |
| 128 | +## Gantt Bound Column Parameters |
| 129 | + |
| 130 | +You can use the following properties on bound columns: |
| 131 | + |
| 132 | +### Data Binding |
| 133 | + |
| 134 | +* `Expandable` - (defaults to `false`) - when set to true, the column shows an expand/collapse arrow in front of the value and denotes hierarchy be intending it. You should set this to at least one column of your treelist to showcase the hierarchical nature of the data. |
| 135 | +* `Field` - (defaults to `null`) - the name of the field in the data source that the column will render as a string (case-sensitive). You can set its as a plain string (`Field="SomeField"`) or to have .NET extract the field name from the model for flat models (`Field=@nameof(MyModelClass.SomeFIeld)`). |
| 136 | + |
| 137 | +### Appearance |
| 138 | + |
| 139 | +* `Title` - the text that is rendered in the column header. See the Notes below for its behavior. |
| 140 | +* `DisplayFormat` - the C# format string that is used to render the field value in the cell when the grid is in display mode. Read more in the [Column Display Format]({%slug treelist-columns-displayformat%}) article. |
| 141 | +* `TextAlign` - specifies the horizontal alignment of the cell text. For example, you can use this property to right-align numeric columns. The property accepts `ColumnTextAlign` enum values (`Left`, `Right` or `Center`). If not set, the text alignment will depend on existing styles on the page, default browser behavior and the text direction. |
| 142 | +* `Visible` - (defaults to `null`) - if this parameter is set to `false` it hides the column from the Gantt Tree. Accepts both `bool` and `bool?` types, and `null` is treated like `true`. |
| 143 | + |
| 144 | +## Notes |
| 145 | + |
| 146 | +* For advanced operations such as filtering and sorting, you *must* set a `Field` to the column, and the field it points to must be a string or a value type (such as a number, string, DateTime, boolean). |
| 147 | + * If a `Field` is not set the column will not allow filtering, sorting and editing for the column. |
| 148 | + * If the `Field` points to a custom object or something like an `IDictionary`, errors will be thrown upon those actions because there are no known data operations on non-primitive types in .NET. |
| 149 | + |
| 150 | +* If you don't set a `Title` for a column, the Gantt will take the `[Display(Name = "My Column Title")]` data annotation attribute from the model field. If that's not available either, the name of the field will be shown. |
| 151 | + |
| 152 | +* If the model has a `[DisplayFormat(DataFormatString = "{0:C}")]` data annotation attribute, the display format will be taken from the format string in the attribute. |
| 153 | + |
| 154 | +* If you want to prevent data mutation for a specific property you can set the `Editable` parameter of the TreeListColumn or the `[Editable]` data annotation attribute to `false` for the desired model field. |
| 155 | + * Columns generated out of model properties that do not have a `setter` or it is not accessible (private) will not be editable too. |
| 156 | + |
| 157 | +* The Gantt uses `Activator.CreateInstance<TItem>();` to generate a new item when an Insert action is invoked, so the Model should have a Parameterless constructor defined. |
0 commit comments