Skip to main content

DataGridView.CustomUnboundData Event

Enables you to populate unbound columns with data, and save changes that users made in unbound columns.

Namespace: DevExpress.Maui.DataGrid

Assembly: DevExpress.Maui.DataGrid.dll

NuGet Package: DevExpress.Maui.DataGrid

Declaration

public event EventHandler<CustomUnboundDataEventArgs> CustomUnboundData

Event Data

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

Property Description
Column Returns the processed unbound column.
IsGetData Indicates that you should supply data for the current cell of the unbound column in the event handler.
IsSetData Indicates that you should save the current value of the unbound column’s cell to a data source in the event handler.
Item Returns an object that is a record in the grid’s underlying data source.
SourceIndex Returns the grid’s row index in the data source.
Value Gets or sets a value of the unbound column’s cell.

Remarks

DataGridView allows you to create unbound (calculated) columns. These columns are not bound to any field in the underlying data source. To provide data for these columns, use the GridColumn.UnboundExpression property or handle the CustomUnboundData event. This event fires in two cases:

  • Provide data for unbound columns
    When the grid is loaded, it raises the CustomUnboundData event with the IsGetData parameter set to true (consequently the IsSetData parameter is set to false). In this case, you can supply data for the currently processed cell. Assign the required value to the Value parameter.
  • Save changes
    When a user modifies an unbound column’s data, the CustomUnboundData event is fired with the IsSetData parameter set to true (consequently IsGetData is set to false). In this case, you can save the modified data value that the Value parameter returns.

Note

The grid raises the CustomUnboundData event only for columns with both the FieldName and UnboundType properties specified.

Example

Assume that the DataGridView instance is bound to a collection of orders. An order has the Product.Name, Product.UnitPrice and Quantity fields. This example shows how to add an unbound column (Total) to the grid to calculate each order amount according to the expression: UnitPrice*Quantity.

Implement logic to calculate column values in one of the following ways:

  • Use the GridColumn.UnboundExpression property:

    <dxg:DataGridView x:Name="grid" ItemsSource="{Binding Orders}">
        <dxg:DataGridView.Columns>
            <dxg:TextColumn FieldName="Product.Name" Caption="Product" Width="170" />
            <dxg:NumberColumn FieldName="Product.UnitPrice" Caption="Price" DisplayFormat="C0"/>
            <dxg:NumberColumn FieldName="Quantity"/>
            <dxg:NumberColumn FieldName="Total" UnboundType="Integer"
                            UnboundExpression="[Quantity] * [Product.UnitPrice]" 
                            IsReadOnly="True" DisplayFormat="C0"/>
        </dxg:DataGridView.Columns>
    </dxg:DataGridView>
    
  • Handle the DataGridView.CustomUnboundData event:

    <dxg:DataGridView x:Name="grid" 
                      ItemsSource="{Binding Orders}" 
                      CustomUnboundData="Grid_CustomUnboundData">
        <dxg:DataGridView.Columns>
            <dxg:TextColumn FieldName="Product.Name" Caption="Product" Width="170" />
            <dxg:NumberColumn FieldName="Product.UnitPrice" Caption="Price" DisplayFormat="C0"/>
            <dxg:NumberColumn FieldName="Quantity"/>
            <dxg:NumberColumn FieldName="Total" UnboundType="Decimal"
                              IsReadOnly="True" DisplayFormat="C0"/>
        </dxg:DataGridView.Columns>
    </dxg:DataGridView>
    
    // Returns the total for a specific row.
    decimal getTotalValue(DataGridView grid, int rowHandle) {
        decimal unitPrice = Convert.ToDecimal(grid.GetCellValue(rowHandle, "Product.UnitPrice"));
        decimal quantity = Convert.ToDecimal(grid.GetCellValue(rowHandle, "Quantity"));
        return unitPrice * quantity;
    }
    
    // Provides data for the Total column.
    void Grid_CustomUnboundData(object sender, CustomUnboundDataEventArgs e) {
        if (e.Column.FieldName == "Total" && e.IsGetData)
            e.Value = getTotalValue((DataGridView)sender, e.SourceIndex);
    }
    
See Also