Unbound Columns
- 4 minutes to read
Bound and Unbound Columns
The GridControl supports bound and unbound columns:
Bound columns obtain their data from data fields in the grid’s data source.
<dxg:GridControl x:Name="grid"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="ProductName"/> <dxg:GridColumn FieldName="UnitPrice"/> <dxg:GridColumn FieldName="UnitsOnOrder"/> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView/> </dxg:GridControl.View> </dxg:GridControl>
Unbound columns are not bound to any field in the data source.
Handle the GridControl.CustomUnboundColumnData / TreeListView.CustomUnboundColumnData event to populate these columns.
<dxg:GridControl x:Name="grid" CustomUnboundColumnData="grid_CustomUnboundColumnData"> <dxg:GridControl.Columns> <!-- --> <dxg:GridColumn FieldName="Total" UnboundType="Boolean" > </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView/> </dxg:GridControl.View> </dxg:GridControl>
private void grid_CustomUnboundColumnData(object sender, GridColumnDataEventArgs e) { if (e.IsGetData) { int price = Convert.ToInt32(e.GetListSourceFieldValue("UnitPrice")); int unitsOnOrder = Convert.ToInt32(e.GetListSourceFieldValue("UnitsOnOrder")); e.Value = price * unitsOnOrder; } }
Tip
GitHub Example: How to Create Unbound Columns
This example shows how to add an unbound column to the GridControl. This column should display the total price, calculated as follows: UnitPrice * UnitsOnOrder.
You can use the ColumnBase.UnboundExpression property to populate an unbound column. The Expressions section describes the syntax for creating expressions.
<dxg:GridColumn FieldName="DiscountAmount" UnboundType="Decimal" UnboundExpression="Round([UnitPrice] * [Quantity] - [Total])"> </dxg:GridColumn>
An unbound column 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 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.
Note
The GridControl.CustomUnboundColumnData / TreeListView.CustomUnboundColumnData events are raised only for columns where both the FieldName and UnboundType properties are specified.
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.
Save Changes
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.
The code sample below uses a Dicitonary to create an editable unbound column:
<dxg:GridControl Name="grid" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True"
ItemsSource="{Binding List}"
CustomUnboundColumnData="grid_CustomUnboundColumnData">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="MyUnboundColumn" UnboundType="String"/>
<!-- ... -->
</dxg:GridControl.Columns>
</dxg:GridControl>
Dictionary<int, string> unboundData = new Dictionary<int, string>();
unboundData[0] = "MyText";
//...
private void grid_CustomUnboundColumnData(object sender, GridColumnDataEventArgs e) {
if (e.Column.FieldName == "MyUnboundColumn") {
// Populate columns
if (e.IsGetData) {
if (unboundData.ContainsKey(e.ListSourceRowIndex))
e.Value = unboundData[e.ListSourceRowIndex];
}
// Post edited values to the underlying data source
if (e.IsSetData && e.Value != null) {
unboundData[e.ListSourceRowIndex] = e.Value.ToString();
}
}
}