Automatically Generated Columns
The Grid allows you to automatically generate a column for each public property of its model rather than defining each column manually.
To enable Automatic Column Generation, set the AutoGenerateColumns parameter of the Grid to true.
The content of this article will be separated into groups for clarity:
Basics
To display all model fields in the grid, just set its AutoGenerateColumns parameter to true.
If you don't need explicitly declared columns (such as a command column or frozen columns) the GridColumns tag is not required.
Use the AutoGenerateColumns parameter to generate columns out of a model
Declare Explicit Columns
The Grid can consist of both Automatically Generated and explicitly declared columns.
The following examples show how you can control their order and positions:
Default Column Order
By default the Automatically Generated Columns are rendered after the manually declared ones.
Observe the default positioning of the Automatically Generated Columns
Define Explicit Column Order
To set explicit position of the Automatically Generated Columns you can use the GridAutoGeneratedColumns tag inside the GridColumns tag and place it in the order you want it to have in relation to the columns you declare.
When the
GridAutoGeneratedColumnstag is used theAutoGenerateColumnsparameter of the Grid must also set totrue.
Observe explicitly set position of the Automatically Generated Columns between a Checkbox and Command columns
Customization
- You can set the
Titleof the column through the[Display(Name="The Desired Title")]attribute on the fields of your model. - You can set the
[Display(AutoGenerateField=false)]attribute over a property of your model if you want to prevent auto-generation of a column from that field. - You can prevent data mutation by setting the
[Editable]attribute tofalseon fields of your model. - The properties that do not have a
setteror thesetteris not accessible (private) will not be editable either. - You can set custom width to all auto-generated columns through the
ColumnWidthparameter ofGridAutoGeneratedColumns. By default they will be equally distributed to fill the width of the Grid.- To enable horizontal scrolling the
ColumnWidthparameter has to be set so that the sum of all columns is greater than the Grid width. You can find more information in the Column Width article.
- To enable horizontal scrolling the
To use the attributes listed above the
System.ComponentModel.DataAnnotationsusing statement has to be present in the file with your model. With Popup editing, you can use validation attributes in addition to the customization attributes.
Example
This example shows how to:
- Use data annotation attributes to
- set the column titles (e.g.,
BoughtBooks) - make a field non-editable (e.g.,
Id) - hide a field from the grid (e.g.,
RegistrationDate) - validate the data (e.g.,
Username)
- set the column titles (e.g.,
- Use the
[Display(AutoGenerateField = false)]attribute to prevent a column from being generated, so you can declare it yourself and customize it (e.g.,RegistrationDate) - Select the order of the columns and set explicit position of the autogenerated Columns
- Make CUD operations
- Make multiple selection
- Add paging to the Grid
Observe the behavior of auto-generated columns with Editing operations, Selection, Paging and using the custom attributes.
Notes
- When the component initializes, it expects to know what columns to render before the data loads.
If the component is bound to a collection of dynamic objects (such as Dictionary or ExpandoObject), the component cannot know what columns to render, due to the nature of these objects. Therefore, the AutoGenerateColumns feature is not applicable in such scenarios. Columns must be declared explicitly or with a loop. Examples of such setups are available at Binding Grid to ExpandoObject.