| |
 |
How to: Add an Unbound Column Storing Arbitrary Data
Assume that the Grid Control is bound to a table that contains the "Quantity", "UnitPrice" and "Discount" columns. The example below shows how to add an unbound column to the grid to display the amount of each order according to the expression: QuantityUnitPrice(1-Discount).
The result is displayed below:

For another example which illustrates working with unbound columns, see the Unbound Columns tutorial.
C# |
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
private void Form1_Load(object sender, System.EventArgs e) {
gridControl1.ForceInitialize();
GridColumn unbColumn = gridView1.Columns.AddField("Total");
unbColumn.VisibleIndex = gridView1.Columns.Count;
unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
unbColumn.OptionsColumn.AllowEdit = false;
unbColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
unbColumn.DisplayFormat.FormatString = "c";
unbColumn.AppearanceCell.BackColor = Color.LemonChiffon;
}
decimal getTotalValue(GridView view, int listSourceRowIndex) {
decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "UnitPrice"));
decimal quantity = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Quantity"));
decimal discount = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Discount"));
return unitPrice * quantity * (1 - discount);
}
private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) {
GridView view = sender as GridView;
if (e.Column.FieldName == "Total" && e.IsGetData) e.Value =
getTotalValue(view, e.ListSourceRowIndex);
}
|
VB |
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Columns
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
gridControl1.ForceInitialize()
Dim unbColumn As GridColumn = GridView1.Columns.AddField("Total")
unbColumn.VisibleIndex = GridView1.Columns.Count
unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal
unbColumn.OptionsColumn.AllowEdit = False
unbColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
unbColumn.DisplayFormat.FormatString = "c"
unbColumn.AppearanceCell.BackColor = Color.LemonChiffon
End Sub
Private Function getTotalValue(view As GridView, listSourceRowIndex As Integer) As Decimal
Dim unitPrice As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "UnitPrice"))
Dim quantity As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Quantity"))
Dim discount As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Discount"))
Return unitPrice * quantity * (1 - discount)
End Function
Private Sub GridView1_CustomUnboundColumnData(ByVal sender As Object, _
ByVal e As CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
Dim view As GridView = TryCast(sender, GridView)
If e.Column.FieldName = "Total" AndAlso e.IsGetData Then
e.Value = getTotalValue(view, e.ListSourceRowIndex)
End If
End Sub
|
Is this topic helpful?
Additional Feedback
Close
|