Skip to main content

Unbound Columns

  • 4 minutes to read

Unbound columns are not bound to a field in the data source. These columns allow you to calculate values based upon values in bound columns or display data from a custom data source.

The functionality of bound and unbound columns in a GridControl are the same. You can sort, group, display summaries, and filter unbound columns in the same manner as bound columns.

Add an Unbound Column

Follow the steps below:

  1. Add a GridColumn object to the GridControl.Columns collection.
  2. Set the UnboundDataType property to the type of values the column should store. The GridControl chooses the column’s default editor based on this property value. To replace the editor, use the ColumnBase.EditSettings property.
  3. Assign a unique field name to the ColumnBase.FieldName property. The field name must not match the field name of another column or the name of a data source field.
  4. Use one of the following techniques to populate the unbound column with data:
<Window ...
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
        xmlns:sys="clr-namespace:System;assembly=mscorlib">
<dxg:GridControl x:Name="grid">
    <!-- ... -->
    <dxg:GridColumn FieldName="Total" 
                    UnboundDataType="{x:Type sys:Decimal}"
                    UnboundExpression="[Quantity] * [UnitPrice]"/>
</dxg:GridControl>
grid.Columns.Add(new DevExpress.Xpf.Grid.GridColumn() { 
    FieldName = "Total", 
    UnboundDataType = typeof(decimal), 
    UnboundExpression = "[Quantity] * [UnitPrice]"
});

You can also use the GridControl.CustomUnboundColumnDataCommand/TreeListView.CustomUnboundColumnDataCommand properties to maintain a clean MVVM pattern and populate unbound columns with data in a ViewModel.

DevExpress WPF | Grid Control - Custom Unbound Data

<dxg:GridControl ItemsSource="{Binding Items}"
                 CustomUnboundColumnDataCommand="{Binding UnboundColumnDataCommand}">
    <!-- ... -->
    <dxg:GridColumn FieldName="Total" 
                    UnboundDataType="{x:Type sys:Decimal}"/>
</dxg:GridControl>
[Command]
public void UnboundColumnData(UnboundColumnRowArgs args) {
    if(args.IsGetData) {
        var item = (Product)args.Item;
        args.Value = item.UnitPrice * item.Quantity;
    }
}

View Example: How to Create Unbound Columns

Edit Unbound Column 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 a user has changed an unbound column’s value, this value should then be saved back to the grid’s data source. To supply data for unbound columns and save any changes 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 Dictionary 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" UnboundDataType="{x:Type sys: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();
        }
    }
} 

Refer to the following help topic for more information: Edit Cell Values in UI.

See Also