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.
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
Open the Data Grid’s smart tag. Click Add Column to create a column.
Select this column and set its GridColumn.FieldName property to a unique string: “DiscountAmount”.
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
Use the GridColumn.UnboundExpression property: click the ellipsis button to open the Expression Editor.
Create a simple expression that multiplies three fields: “Quantity”, “Unit Price”, “Discount”.
Tip
Read the following help topic for more information on expression syntax: Criteria Language Syntax.
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
Create another column. Set its GridColumn.FieldName to “Total” and GridColumn.UnboundDataType to
System.Decimal
.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).
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 thee.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.