Skip to main content

DataGridView.CustomUnboundColumnData Event

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

Namespace: DevExpress.XamarinForms.DataGrid

Assembly: DevExpress.XamarinForms.Grid.dll

NuGet Package: DevExpress.XamarinForms.Grid

Declaration

public event GridColumnDataEventHandler CustomUnboundColumnData

Event Data

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

Property Description
Column Gets the currently processed unbound column.
IsGetData Indicates that you should provide 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 Gets an object that represents a record in the grid’s underlying data source.
RowHandle Gets the grid’s row handle.
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 CustomUnboundColumnData event. This event fires in two cases:

  • Provide data for unbound columns
    When the grid is loaded, it raises the CustomUnboundColumnData 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 CustomUnboundColumnData 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.

You can identify the currently processed cell by the column (Column) and row (RowHandle).

Note

The grid raises the CustomUnboundColumnData 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.CustomUnboundColumnData event:

    <dxg:DataGridView x:Name="grid" 
                      ItemsSource="{Binding Orders}" 
                      CustomUnboundColumnData="Grid_CustomUnboundColumnData">
        <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_CustomUnboundColumnData(object sender, GridColumnDataEventArgs e) {
        if (e.Column.FieldName == "Total" && e.IsGetData)
            e.Value = getTotalValue((DataGridView)sender, e.RowHandle);
    }
    
See Also