Creating Columns and Binding Them to Data Properties
- 3 minutes to read
When the GridControl is bound to a data source, you need to create columns and bind them to data properties. There are two ways to do this:
Automatically create columns for all properties in a data source
The grid automatically generates columns for all properties in the data source. The order of columns is the same as the order of properties in the data source. The grid’s GridControl.Columns collection is populated at runtime. This collection is empty at design time.
This behavior is controlled by the DataControlBase.AutoGenerateColumns property. One of the AutoGenerateColumnsMode enum values is used to set this property. After all columns have been created and added to the collection, the grid fires the DataControlBase.AutoGeneratedColumns event.
This can be useful, for instance, when the structure of the underlying data source is unknown (e.g. switching between data tables).
Create columns and bind them to data manually
Manually create all the necessary columns, add them to the GridControl.Columns collection and bind them to data source properties using their ColumnBase.FieldName or ColumnBase.Binding property.
Note
The ColumnBase.FieldName or ColumnBase.Binding (that also sets ColumnBase.FieldName internally) properties should be defined for every GridControl column.
Tip
Refer to the Binding Columns to Data Source Fields article to learn about difference between the ColumnBase.FieldName and ColumnBase.Binding property.
Creating and Binding Columns at Design Time
To access the grid’s GridControl.Columns collection, invoke the Columns edit form:
This form allows you to add, delete, access and customize column settings, and perform other common collection management tasks.
The following code shows how to create columns in XAML:
using the GridColumn.FieldName property.
<dxg:GridControl x:Name="grid"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="ProductName"/> <dxg:GridColumn FieldName="UnitPrice"/> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView /> </dxg:GridControl.View> </dxg:GridControl>
using the GridColumn.Binding property.
<dxg:GridControl x:Name="grid"> <dxg:GridControl.Columns> <dxg:GridColumn Header="Unit Price" Binding="{Binding UnitPrice, Mode=TwoWay}"/> <dxg:GridColumn Header="Product Name" Binding="{Binding ProductName, Mode=TwoWay}"/> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView /> </dxg:GridControl.View> </dxg:GridControl>
Note
When working with data structures that contain Dynamic Objects, use the ColumnBase.Binding property instead of ColumnBase.FieldName. Learn more: Binding Columns to Data Source Fields.
Note
Columns that are bound using the ColumnBase.Binding property might work more slowly than columns that use the ColumnBase.FieldName property. Learn more: Binding Columns to Data Source Fields.
Creating and Binding Columns at Runtime
The following example shows how to create a column and bind it to a data property:
using the GridColumn.FieldName property.
using the GridColumn.Binding property.
To create columns for all properties in a data source, call the DataControlBase.PopulateColumns method. This method clears the grid’s GridControl.Columns collection, and re-populates it with columns/card fields that correspond to all properties in a data source.