Skip to main content

Tutorial: Unbound Columns

  • 4 minutes to read

This tutorial explains how to:

  • Create an unbound column at design time.

  • Specify an expression for an unbound column at design time.

  • Edit an expression at runtime.

  • Supply data to an unbound column in code.

  • Edit cell values in an unbound column and save changes.

Watch Video: Unbound Columns

Starting Point

An application with a Data Grid bound to the “Order Details” table of the Northwind database.

WinForms Data Grid bound to Order Details table Northwind database

Create an Unbound Column at Design Time

  1. Open the Data Grid’s smart tag. Click Add Column to create a column.

    WinForms Data Grid create column in the smart tag

  2. Select this column and set its GridColumn.FieldName property to a unique string: “DiscountAmount”.

    WinForms Data Grid set GridColumn.FieldName property for column

  3. Set the column’s GridColumn.UnboundDataType property to a valid data type. For this tutorial, use the System.Decimal value.

    WinForms Data Grid set GridColumn.UnboundDataType property for column

Specify an Expression at Design Time

  1. Use the GridColumn.UnboundExpression property: click the ellipsis button to open the Expression Editor.

    WinForms Data Grid open Expression Editor for unbound column

  2. Create a simple expression that multiplies three fields: “Quantity”, “Unit Price”, “Discount”.

    WinForms Data Grid create unbound expression in Expression Editor

    Tip

    Read the following help topic for more information on expression syntax: Criteria Language Syntax.

  3. Set the OptionsColumn.AllowEdit property to false to make this column read-only.

    WinForms Data Grid set GridColumn.UnboundDataType property for column

Tip

Set the column’s FormatInfo.FormatType to FormatType.Numeric and FormatInfo.FormatString to c2 to format column values as currency.

Edit an Expression for an Unbound Column at Runtime

Set the column’s GridColumn.ShowUnboundExpressionMenu property to true to allow a user to modify an expression for an unbound column at runtime.

WinForms Data Grid set ShowUnboundExpressionMenu property for column

A user can invoke the Expression Editor from the column’s context menu at runtime to change the expression.

WinForms Data Grid invoke Expression Editor at runtime from column context menu

Supply Data to an Unbound Column on an Event

  1. Create another column. Set its GridColumn.FieldName to “Total” and GridColumn.UnboundDataType to System.Decimal.

  2. Select “gridView1” and subscribe to the ColumnView.CustomUnboundColumnData event on the “Events” page of the Properties panel.

    Note

    The ColumnView.CustomUnboundColumnData event fires each time a column value is about to be displayed and after a column cell is modified (when data posting is required).

  3. Use the ColumnView.GetListSourceRowCellValue method to retrieve values of Quantity, UnitPrice, and Discount columns if the e.IsGetData event parameter is true. Calculate a value for the unbound column and assign it to the e.Value event parameter.

    void gridView_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
            GridView view = sender as GridView;
            if(view == null) return;
            int rowIndex = e.ListSourceRowIndex;
            decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "UnitPrice"));
            decimal quantity = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Quantity"));
            decimal discount = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Discount"));
            if (e.Column.FieldName != "Total") return;
            if (e.IsGetData)
                    e.Value = unitPrice * quantity * (1 - discount);
    }
    

Edit Cell Values in an Unbound Column

On editing, you need to save changes in unbound columns. You can use the ColumnView.CustomUnboundColumnData event for this purpose.

The following code saves changes made in Total column cells to a dictionary. The e.IsSetData event parameter indicates whether a cell value in an unbound column was modified.

Dictionary<int, decimal> customTotals = new Dictionary<int, decimal>();

private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
    GridView view = sender as GridView;
    if(view == null) return;
    int rowIndex = e.ListSourceRowIndex;
    decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "UnitPrice"));
    decimal quantity = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Quantity"));
    decimal discount = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Discount"));
    if (e.Column.FieldName != "Total") return;
    if (e.IsGetData) {
        if (!customTotals.ContainsKey(rowIndex))
            customTotals.Add(rowIndex, unitPrice * quantity * (1 - discount));
        e.Value = customTotals[rowIndex];
    }
    if (e.IsSetData) {
        customTotals[rowIndex] = Convert.ToDecimal(e.Value);
    }
}

Tip

Read the following topic for more information: Unbound Columns.

See Also