Back to Devexpress

GridControl.CustomUnboundColumnData Event

wpf-devexpress-dot-xpf-dot-grid-dot-gridcontrol-30364296.md

latest9.8 KB
Original Source

GridControl.CustomUnboundColumnData Event

Allows you to populate unbound columns with data.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event GridColumnDataEventHandler CustomUnboundColumnData
vb
Public Event CustomUnboundColumnData As GridColumnDataEventHandler

Event Data

The CustomUnboundColumnData event's data class is GridColumnDataEventArgs. The following properties provide information specific to this event:

PropertyDescription
ColumnGets the unbound column currently being processed.
IsGetDataGets whether you should provide data for the currently processed cell. Inherited from ColumnDataEventArgsBase.
IsSetDataGets whether a cell’s value should be saved to a data source. Inherited from ColumnDataEventArgsBase.
ListSourceRowIndexGets the index of the record in a data source to which the processed row corresponds.
SourceGets the grid control that raised the event.
ValueGets or sets the processed cell’s value. Inherited from ColumnDataEventArgsBase.

The event data class exposes the following methods:

MethodDescription
GetListSourceFieldValue(Int32, String)Returns the value of the specified cell within the specified row in the grid’s data source.
GetListSourceFieldValue(String)Returns the value of the specified cell within the processed row in the grid’s data source.

Remarks

Unbound Columns are not bound to any field in the data source. You can calculate unbound column values based on values of bound columns or populate unbound columns with data from a custom data source.

Handle the CustomUnboundColumnData event to populate unbound columns with data and save changes to a custom data source.

Display Unbound DataThe GridColumnDataEventArgs.IsGetData property returns true when the GridControl populates unbound columns with data. The GridColumnDataEventArgs.ListSourceRowIndex property returns the processed row’s index in a grid’s data source. Specify the GridColumnDataEventArgs.Value property to display data in the unbound column.Save ChangesThe GridColumnDataEventArgs.IsSetData property returns true when a user changes a cell value. The GridColumnDataEventArgs.Value property returns the modified cell value that you can save to a custom data source.

If you want to maintain a clean MVVM pattern and populate unbound columns with data in a View Model, create a command and bind it to the CustomUnboundColumnDataCommand property.

Note

The GridControl does not raise the CustomUnboundColumnData event if you use the TreeList View. Handle the TreeListView.CustomUnboundColumnData event instead.

Example

This example shows how to add an unbound column to the GridControl. This column should display the total price, calculated as follows: UnitPrice * UnitsOnOrder.

View Example: How to Create Unbound Columns

xaml
<dxg:GridControl x:Name="grid"
                 CustomUnboundColumnData="grid_CustomUnboundColumnData">
    <dxg:GridColumn FieldName="CompanyName"/>
    <dxg:GridColumn FieldName="City"/>
    <dxg:GridColumn FieldName="UnitPrice">
        <dxg:GridColumn.EditSettings>
            <dxe:TextEditSettings DisplayFormat="c2"/>
        </dxg:GridColumn.EditSettings>
    </dxg:GridColumn>
    <dxg:GridColumn FieldName="Quantity"/>
    <dxg:GridColumn FieldName="Total" UnboundDataType="{x:Type sys:Decimal}" ReadOnly="True">
        <dxg:GridColumn.EditSettings>
            <dxe:TextEditSettings DisplayFormat="c2"/>
        </dxg:GridColumn.EditSettings>
    </dxg:GridColumn>
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True"/>
    </dxg:GridControl.View>
</dxg:GridControl>
cs
void grid_CustomUnboundColumnData(object sender, GridColumnDataEventArgs e) {
    if(e.IsGetData) {
        int price = Convert.ToInt32(e.GetListSourceFieldValue(nameof(Product.UnitPrice)));
        int quantity = Convert.ToInt32(e.GetListSourceFieldValue(nameof(Product.Quantity)));
        e.Value = price * quantity;
    }
}
vb
Private Sub grid_CustomUnboundColumnData(ByVal sender As Object, ByVal e As GridColumnDataEventArgs)
    If e.IsGetData Then
        Dim price As Integer = Convert.ToInt32(e.GetListSourceFieldValue(NameOf(Product.UnitPrice)))
        Dim quantity As Integer = Convert.ToInt32(e.GetListSourceFieldValue(NameOf(Product.Quantity)))
        e.Value = price * quantity
    End If
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references to the CustomUnboundColumnData event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-data-grid-create-unbound-columns/CS/DXGrid_UnboundColumns_CodeBehind/Window1.xaml#L9

xml
<dxg:GridControl x:Name="grid"
                 CustomUnboundColumnData="grid_CustomUnboundColumnData">
    <dxg:GridColumn FieldName="CompanyName"/>

wpf-data-grid-display-icon-in-unbound-column-based-on-value-in-bound-column/CS/UnboundColumnWithIcons_CodeBehind/Window1.xaml#L16

xml
<dxg:GridControl x:Name="grid"
                 CustomUnboundColumnData="GridControl_CustomUnboundColumnData">
    <dxg:GridControl.Columns>

wpf-data-grid-change-background-color-for-modified-cells/CS/HighlightChangedCellBehavior.cs#L24

csharp
base.OnAttached();
AssociatedObject.CustomUnboundColumnData += OnGridCustomUnboundColumnData;
AssociatedObject.Loaded += OnGridLoaded;

wpf-data-grid-change-background-color-for-modified-cells/VB/HighlightChangedCellBehavior.vb#L40

vb
MyBase.OnAttached()
AddHandler Me.AssociatedObject.CustomUnboundColumnData, AddressOf Me.OnGridCustomUnboundColumnData
AddHandler Me.AssociatedObject.Loaded, AddressOf Me.OnGridLoaded

See Also

Unbound Columns

TreeListView.CustomUnboundColumnData

How to Display an Icon in an Unbound Column Based on the Value in a Bound Column

GridControl Class

GridControl Members

DevExpress.Xpf.Grid Namespace