wpf-6124-controls-and-libraries-data-grid-grid-view-data-layout-columns-and-card-fields-unbound-columns.md
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.
Follow the steps below:
Specify an expression (ColumnBase.UnboundExpression) that calculates column values. The Expressions section describes supported syntax.
Handle the GridControl.CustomUnboundColumnData/TreeListView.CustomUnboundColumnData event to specify unbound column values.
<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]"
});
grid.Columns.Add(New DevExpress.Xpf.Grid.GridColumn() With {
.FieldName = "Total",
.UnboundDataType = GetType(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.
<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;
}
}
<Command>
Public Sub UnboundColumnData(ByVal args As UnboundColumnRowArgs)
If args.IsGetData Then
Dim item = CType(args.Item, Product)
args.Value = item.UnitPrice * item.Quantity
End If
End Sub
View Example: How to Create Unbound Columns
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 DataThe 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 ChangesThe 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();
}
}
}
Dictionary<int, string> unboundData = new Dictionary<int, string>();
unboundData[0] = "MyText";
' ...
Private Sub grid_CustomUnboundColumnData(ByVal sender As Object, ByVal e As GridColumnDataEventArgs)
If e.Column.FieldName = "MyUnboundColumn" Then
' Populate columns
If e.IsGetData Then
If unboundData.ContainsKey(e.ListSourceRowIndex) Then e.Value = unboundData(e.ListSourceRowIndex)
End If
' Post edited values to the underlying data source
If e.IsSetData AndAlso e.Value IsNot Nothing Then
unboundData(e.ListSourceRowIndex) = e.Value.ToString()
End If
End If
End Sub
Refer to the following help topic for more information: Edit Cell Values in UI.
See Also
How to Display an Icon in an Unbound Column Based on the Value in a Bound Column