- 4 minutes to read
Bound and Unbound Columns
The DXGrid supports bound and unbound columns. Bound columns obtain their data from data fields in a grid’s data source. Unbound columns are not bound to any field in the data source. These columns must be populated manually by handling the GridControl.CustomUnboundColumnData event (TreeListView.CustomUnboundColumnData in a TreeListView) or using the ColumnBase.UnboundExpression property. The Expressions section describes the syntax for creating expressions.
An unbound column, however, is one that meets the following two requirements:
- The ColumnBase.FieldName property must be set to a unique value, and not refer to any field in the grid’s data source.
- The ColumnBase.UnboundType property must be set to an appropriate value according to the type of data this column is supposed to display (Boolean, DateTime, Decimal, Integer, String or Object). This property also determines an unbound column’s default editor, used to represent its values. For example, if it is set to Boolean, a CheckEdit will be used by default. To replace the default editor, use the ColumnBase.EditSettings property.
There is no difference between working with bound and unbound columns. You can sort, group, display summaries and filter unbound columns in the same manner as bound columns.
If unbound data is obtained from a custom data source, when adding a new row you should add a new entry to the custom data source that corresponds to the new record in the grid. Similarly, when a record is deleted, delete the corresponding entry in the custom data source. To receive notifications that a record has been added or removed, use the methods provided by a data source.
Unbound Column’s Data
In most instances, data for unbound columns is obtained from a custom data source or is calculated based upon the values of bound columns.
Unbound data can be edited if it is retrieved from a custom data source. After an end-user has changed an unbound column’s value, this value should then be saved back to the grid’s data source. By default, data editing is enabled. To make an unbound column read-only, set its ColumnBase.ReadOnly property to true. You can also disable data editing by setting the ColumnBase.AllowEditing property to ‘False’.
To provide data for unbound columns and save any changes made back to a custom data source, handle the GridControl.CustomUnboundColumnData event. Note that this event is raised only for unbound columns.
Display Unbound Data
The event parameter’s ColumnDataEventArgsBase.IsGetData property returns true. The event is raised for each data row, allowing the values of unbound columns to be specified. The processed row’s index in a grid’s data source is returned by the GridColumnDataEventArgs.ListSourceRowIndex property. A value should be assigned to the ColumnDataEventArgsBase.Value property.
The event parameter’s ColumnDataEventArgsBase.IsSetData property returns true. The ColumnDataEventArgsBase.Value property contains the modified cell value that should be saved to a custom data source.
Example: Display Unbound Data
This example shows how to add an unbound column to the DXGrid control. This column should display the total price, calculated as follows: UnitPrice * UnitsOnOrder.
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-create-unbound-columns-e1503.
<Window x:Class="DXGrid_UnboundColumns.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" Title="Window1" Height="300" Width="552"> <Grid> <dxg:GridControl x:Name="grid" CustomUnboundColumnData="grid_CustomUnboundColumnData"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="ProductName" /> <dxg:GridColumn FieldName="UnitPrice"> <dxg:GridColumn.EditSettings> <dxe:TextEditSettings DisplayFormat="c2" /> </dxg:GridColumn.EditSettings> </dxg:GridColumn> <dxg:GridColumn FieldName="UnitsOnOrder" /> <dxg:GridColumn FieldName="Total" UnboundType="Boolean" ReadOnly="True"> <dxg:GridColumn.EditSettings> <dxe:TextEditSettings DisplayFormat="c2" /> </dxg:GridColumn.EditSettings> </dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView AutoWidth="True"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>