Skip to content

Commit 9160601

Browse files
Merge pull request #3756 from syncfusion-content/dataannotation_Hotfix
[HotFix 968712] Prepared the UG for the SfDataGrid data annotation support.
2 parents a07bcbc + 573f414 commit 9160601

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
5.63 KB
Loading
5.51 KB
Loading
8.24 KB
Loading

MAUI/DataGrid/columns.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,152 @@ private void SfDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGenerati
148148
{% endhighlight %}
149149
{% endtabs %}
150150

151+
### Data Annotations with AutoGenerateColumns
152+
153+
SfDataGrid support to generate the columns based on built-in [Data Annotation Attributes](https://learn.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/cc490428(v=vs.95)).
154+
155+
N> Data annotations are only applied when the `DataGrid.AutoGenerateColumns` property is set to True.
156+
157+
#### Exclude column
158+
159+
You can skip the column generation using `AutoGenerateField` property.
160+
161+
{% tabs %}
162+
{% highlight c# %}
163+
[Display(AutoGenerateField = false, Description = "OrderID field is not generated in UI")]
164+
public int OrderID
165+
{
166+
get { return orderID; }
167+
set { orderID = value; }
168+
}
169+
{% endhighlight %}
170+
{% endtabs %}
171+
172+
#### Editing
173+
174+
You can enable editing of cell values by setting the `Editable` attribute to true.
175+
176+
{% tabs %}
177+
{% highlight c# %}
178+
[Editable(true)]
179+
public string Country
180+
{
181+
get { return country; }
182+
set { country = value; }
183+
}
184+
{% endhighlight %}
185+
{% endtabs %}
186+
187+
#### Change the HeaderText of column
188+
189+
You can customize header text of column using `Display.Name` property or `Display.ShortName` property.
190+
191+
{% tabs %}
192+
{% highlight c# %}
193+
[Display(Name="Name of the Customer",Description="CustomerName is necessary for identification ")]
194+
public string CustomerName
195+
{
196+
get { return customerName; }
197+
set { customerName = value; }
198+
}
199+
{% endhighlight %}
200+
{% endtabs %}
201+
202+
#### Change the order of the columns
203+
204+
You can change the order of columns using the `Display.Order` property. Columns are arranged based on the specified order value, with lower values appearing first.
205+
206+
{% tabs %}
207+
{% highlight c# %}
208+
[Display(Order=1)]
209+
public string CustomerID
210+
{
211+
get { return customerId; }
212+
set { customerId = value; }
213+
}
214+
215+
[Display(Order=0)]
216+
public int OrderID
217+
{
218+
get { return orderID; }
219+
set { orderID = value; }
220+
}
221+
{% endhighlight %}
222+
{% endtabs %}
223+
224+
The OrderID and CustomerID column rearranged based on specified order.
225+
226+
<img alt="Changing Columns Order in Maui DataGrid" src="Images\columns\maui-datagrid-order.png" width="404"/>
227+
228+
#### DataGrid read-only column
229+
230+
You can disable the editing for a column using `ReadOnly` attribute.
231+
232+
{% tabs %}
233+
{% highlight c# %}
234+
[ReadOnly(true)]
235+
public string Country
236+
{
237+
get { return country; }
238+
set { country = value; }
239+
}
240+
{% endhighlight %}
241+
{% endtabs %}
242+
243+
#### Format datagrid columns using DisplayFormat attribute
244+
245+
You can format auto-generated columns using the [DisplayFormat](https://learn.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/cc679253%28v%3dvs.95%29) attribute with the [DataFormatString](https://learn.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/cc679306%28v%3dvs.95%29) property defined for properties in the model.
246+
247+
{% tabs %}
248+
{% highlight c# %}
249+
[DisplayFormat(DataFormatString = "yyyy")]
250+
public DateTime OrderDate
251+
{
252+
get { return _orderDate; }
253+
set { orderDate = value; }
254+
}
255+
256+
[DisplayFormat(DataFormatString = "Country is {0}")]
257+
public string Country
258+
{
259+
get { return country; }
260+
set { country = value; }
261+
}
262+
{% endhighlight %}
263+
{% endtabs %}
264+
265+
<img alt="Maui DataGrid with Columns Formatting" src="Images\columns\maui-datagrid-formatting.png" width="404"/>
266+
267+
#### Group columns under stacked header
268+
You can group multiple columns under a shared stacked header using the `Display.GroupName` property. Nested grouping is supported using the / separator in the ChildColumns property.
269+
270+
{% tabs %}
271+
{% highlight c# %}
272+
[Display(GroupName = "Order Details")]
273+
public string? OrderID
274+
{
275+
get { return orderID; }
276+
set { this.orderID = value; }
277+
}
278+
279+
[Display(GroupName = "Order Details")]
280+
public DateTime OrderDate
281+
{
282+
get { return _orderDate; }
283+
set { _orderDate = value; }
284+
}
285+
286+
[Display(GroupName = "Order Details")]
287+
public string? ShipCountry
288+
{
289+
get { return shipCountry; }
290+
set { this.shipCountry = value; }
291+
}
292+
{% endhighlight %}
293+
{% endtabs %}
294+
295+
<img alt="Maui DataGrid group columns with stacked header" src="Images\columns\maui-datagrid-groupName.png" width="404"/>
296+
151297
## Manually generate columns
152298

153299
The `SfDataGrid` allows to define the columns manually by adding the [DataGridColumn](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.DataGridColumn.html) objects to the [SfDataGrid.Columns](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.SfDataGrid.html#Syncfusion_Maui_DataGrid_SfDataGrid_Columns) collection. If you want to show only the manually defined columns in the view, you can achieve that by setting the `SfDataGrid.AutoGenerateColumnsMode` property to `None`.
@@ -238,3 +384,4 @@ this.dataGrid.Columns.RemoveAt(1);
238384
{% endtabs %}
239385

240386

387+

0 commit comments

Comments
 (0)