windowsforms-114678-controls-and-libraries-data-grid-getting-started-walkthroughs-data-binding-and-working-with-columns-tutorial-unbound-columns.md
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.
An application with a Data Grid bound to the “Order Details” table of the Northwind database.
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.
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”.
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.
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.
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.
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.
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);
}
}
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