Binding Columns to Data Source Fields
- 3 minutes to read
You can use the ColumnBase.FieldName and ColumnBase.Binding properties to associate a grid column with a data source field. This topic describes the difference between these properties.
The ColumnBase.FieldName or ColumnBase.Binding properties should be defined for every GridControl column.
Note
When you use a custom CellTemplate, follow the recommendations below to automatically bind cell values to the property specified by FieldName
/ Binding
:
- If the template contains a BaseEdit class descendant, use the solution from the following topic: Custom In-Place Cell Editors.
- If the template does not include a BaseEdit class descendant, use the following paths to bind it to data: Data Binding.
The FieldName Property
The FieldName property exhibits better performance than the Binding property because the GridControl obtains property values using PropertyDescriptor objects. The following limitations apply when you use the FieldName property:
- A column cannot be used for Dynamic Object properties (binding to the ExpandoObject is supported).
- A column cannot access collection members (like FieldName=”SomeItems[0]”).
- Only objects of the same type in the ItemsSource collection are supported. PropertyDescriptor objects obtained from the collection type or the first data source item are internally cached and used for all data source items.
- A column ignores a nested property’s changes when the field name has a complex path (like “ClientClasses.Count“). To avoid this limitation, set the DataControlBase.DetectNestedPropertyChanges property to true.
- Collection properties cannot be edited.
The Binding Property
You can use the Binding property to avoid the FieldName property’s limitations. The following limitations apply when you use the Binding property:
- The sort and filter operations are slower because a column obtains its cell values using the standard binding mechanism.
- Columns are unbound columns, and all limitations related to unbound columns (see Server Mode Limitations and Binding to ICollectionView) apply to them.
Columns with the specified Binding property are read-only. Set the binding’s Mode property to TwoWay to allow users to edit values within these columns.
Note
You cannot use the Binding and FieldName properties simultaneously if they refer to an existing data source property.
How the GridControl Identifies Columns
The GridControl identifies columns by their FieldName values. If you use Binding, the FieldName value is set automatically and has the “RowData.Row.{Your binding path}” format. You can pass this value to the GridSortInfo constructor, specify it as the summary item’s FieldName, use it in conditional formatting rules, etc.
You can set the FieldName property to any unique value and use this value instead of “RowData.Row.{Binding Path}” as shown in the code sample below:
<dxg:GridColumn Binding="{Binding Id}" FieldName="fakeFieldName1"/>
<dxg:GridColumn Binding="{Binding Name}" />
grid.SortInfo.Add(new GridSortInfo("fakeFieldName1", ListSortDirection.Descending));
grid.SortInfo.Add(new GridSortInfo("RowData.Row.Name", ListSortDirection.Ascending));
If you have multiple columns with the same binding path, set their FieldName to unique values to enable the GridControl to distinguish between columns:
<dxg:GridColumn Binding="{Binding Id, Converter={StaticResource converter1}}" FieldName="c1"/>
<dxg:GridColumn Binding="{Binding Id, Converter={StaticResource converter2}}" FieldName="c2"/>