Back to Devexpress

Tutorial: Unbound Columns

windowsforms-114678-controls-and-libraries-data-grid-getting-started-walkthroughs-data-binding-and-working-with-columns-tutorial-unbound-columns.md

latest6.5 KB
Original Source

Tutorial: Unbound Columns

  • Sep 27, 2024
  • 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.

Create an Unbound Column at Design Time

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

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

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

Specify an Expression at Design Time

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

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

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

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.

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

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.

  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.

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.

csharp
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);
    }
}
vb
Private customTotals As New Dictionary(Of Integer, Decimal)()

Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    If view Is Nothing Then
        Return
    End If
    Dim rowIndex As Integer = e.ListSourceRowIndex
    Dim unitPrice As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "UnitPrice"))
    Dim quantity As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Quantity"))
    Dim discount As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Discount"))
    If e.Column.FieldName <> "Total" Then
        Return
    End If
    If e.IsGetData Then
        If Not customTotals.ContainsKey(rowIndex) Then
            customTotals.Add(rowIndex, unitPrice * quantity * (1 - discount))
        End If
        e.Value = customTotals(rowIndex)
    End If
    If e.IsSetData Then
        customTotals(rowIndex) = Convert.ToDecimal(e.Value)
    End If
End Sub

Tip

Read the following topic for more information: Unbound Columns.

See Also

Data Binding

How to: Display Images from a Data Field with Image Paths