windowsforms-devexpress-dot-xtraverticalgrid-dot-vgridcontrol-719a9139.md
Enables data to be provided to and modified data to be saved from unbound rows.
Namespace : DevExpress.XtraVerticalGrid
Assembly : DevExpress.XtraVerticalGrid.v25.2.dll
NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.VerticalGrid
public event CustomDataEventHandler CustomUnboundData
Public Event CustomUnboundData As CustomDataEventHandler
The CustomUnboundData event's data class is CustomDataEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| IsGetData | Gets whether the column/row needs to be populated with cell values. Inherited from UnboundColumnDataEventArgs. |
| IsSetData | Gets whether a cell value changed and data needs to be posted to a data source. Inherited from UnboundColumnDataEventArgs. |
| ListSourceRowIndex | Gets the column’s index in the data source. |
| RecordObject | Returns an object in the bound data source that contains data for the processed column. |
| Row | Gets the unbound row. |
| RowProperties | Gets a row that contains a cell currently being processed. |
| Value | Gets or sets a cell value in an unbound column. Inherited from UnboundColumnDataEventArgs. |
| ValueType | Gets the value of the UnboundDataType property. |
The CustomUnboundData event is fired for unbound rows. To create an unbound row, create a row object and set its RowProperties.UnboundDataType property to the type of data the row is supposed to display. This row’s field name must be unique and it must not refer to any field in the control’s underlying data source.
The CustomUnboundData event is fired in two cases:
The currently processed cell is identified by the row and record index. The Row parameter refers to the row. To identify the record, use the ListSourceRowIndex parameter.
Note
If you need to get or set specific cell values while handling the CustomUnboundData event, use methods provided by the bound data source. The event’s ListSourceRowIndex parameter allows you to identify the current data record.
Do not use methods provided by the grid control to get/set cell values (for instance, VGridControlBase.GetCellValue and VGridControlBase.SetCellValue).
This example demonstrates how to create an editable unbound row. It uses a simple cache implementation within the CustomUnboundData event handler to fetch custom data faster.
using DevExpress.XtraVerticalGrid.Rows;
private void CreateUnboundRow() {
EditorRow row = vGridControl1.Rows.AddEditorRow("UnboundRow");
row.Properties.UnboundDataType = typeof(string);
}
Dictionary<int, string> storage = new Dictionary<int, string>();
private void vGridControl1_CustomUnboundData(object sender, DevExpress.XtraVerticalGrid.Events.CustomDataEventArgs e) {
if(e.RowProperties.FieldName == "UnboundRow") {
if(e.IsGetData)
if(storage.ContainsKey(e.ListSourceRowIndex))
e.Value = storage[e.ListSourceRowIndex];
else
e.Value = storage[e.ListSourceRowIndex] = string.Format("Unbound value {0}", e.ListSourceRowIndex);
if(e.IsSetData)
storage[e.ListSourceRowIndex] = e.Value.ToString();
}
}
Imports DevExpress.XtraVerticalGrid.Rows
Private Sub CreateUnboundRow()
Dim row As EditorRow = vGridControl1.Rows.AddEditorRow("UnboundRow")
row.Properties.UnboundDataType = GetType(String)
End Sub
Private storage As New Dictionary(Of Integer, String)()
Private Sub vGridControl1_CustomUnboundData(ByVal sender As Object, ByVal e As DevExpress.XtraVerticalGrid.Events.CustomDataEventArgs)
If e.RowProperties.FieldName = "UnboundRow" Then
If e.IsGetData Then
If storage.ContainsKey(e.ListSourceRowIndex) Then
e.Value = storage(e.ListSourceRowIndex)
Else
storage(e.ListSourceRowIndex) = String.Format("Unbound value {0}", e.ListSourceRowIndex)
e.Value = storage(e.ListSourceRowIndex)
End If
End If
If e.IsSetData Then
storage(e.ListSourceRowIndex) = e.Value.ToString()
End If
End If
End Sub
In the following example, it is assumed that the Vertical Grid is bound to a table that contains the “Quantity”, “UnitPrice” and “Discount” fields. The code below adds an unbound row that calculates the total order amount according to the following expression: Quantity*UnitPrice*(1-Discount).
using DevExpress.XtraVerticalGrid;
using DevExpress.XtraVerticalGrid.Rows;
using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.XtraVerticalGrid.Events;
// Unbound row.
EditorRow rowTotal = null;
private void Form1_Load(object sender, EventArgs e) {
// ...
vGridControl1.CustomUnboundData += new CustomDataEventHandler(vGridControl1_CustomUnboundData);
// Create and initialize the unbound Total row.
rowTotal = new EditorRow();
rowTotal.Properties.Caption = "Total";
rowTotal.Properties.FieldName = "Total";
// Specify format settings.
rowTotal.Properties.Format.FormatType = FormatType.Numeric;
rowTotal.Properties.Format.FormatString = "c";
// Disable edit operations.
rowTotal.Properties.ReadOnly = true;
vGridControl1.Rows.Add(rowTotal);
rowTotal.Properties.UnboundDataType = typeof(decimal);
// Customize the appearance settings.
rowTotal.Appearance.BackColor = Color.FromArgb(179, 226, 221);
rowTotal.Appearance.Font = new System.Drawing.Font(rowTotal.Appearance.Font,
System.Drawing.FontStyle.Bold);
}
// Provide data for the Total row.
private void vGridControl1_CustomUnboundData(object sender, CustomDataEventArgs e) {
VGridControl vGrid = sender as VGridControl;
if (e.Row == rowTotal && e.IsGetData) {
DataRowView row = (DataRowView)vGrid.GetRecordObject(e.ListSourceRowIndex);
decimal unitPrice = Convert.ToDecimal(row["UnitPrice"]);
decimal quantity = Convert.ToDecimal(row["Quantity"]);
decimal discount = Convert.ToDecimal(row["Discount"]); ;
e.Value = unitPrice * quantity * (1 - discount);
}
}
Imports DevExpress.XtraVerticalGrid
Imports DevExpress.XtraVerticalGrid.Rows
Imports DevExpress.Data
Imports DevExpress.Utils
Imports DevExpress.XtraVerticalGrid.Events
' Unbound row.
Dim rowTotal As EditorRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'...
AddHandler VGridControl1.CustomUnboundData, AddressOf VGridControl1_CustomUnboundData
' Create and initialize the unbound Total row.
rowTotal = New EditorRow()
rowTotal.Properties.Caption = "Total"
rowTotal.Properties.FieldName = "Total"
' Specify format settings.
rowTotal.Properties.Format.FormatType = FormatType.Numeric
rowTotal.Properties.Format.FormatString = "c"
' Disable edit operations.
rowTotal.Properties.ReadOnly = True
VGridControl1.Rows.Add(rowTotal)
rowTotal.Properties.UnboundDataType = GetType(Decimal)
' Customize the appearance settings.
rowTotal.Appearance.BackColor = Color.FromArgb(179, 226, 221)
rowTotal.Appearance.Font = New System.Drawing.Font(rowTotal.Appearance.Font, _
System.Drawing.FontStyle.Bold)
End Sub
' Provide data for the Total row.
Private Sub VGridControl1_CustomUnboundData(ByVal sender As System.Object, _
ByVal e As CustomDataEventArgs)
Dim vGrid As VGridControl = sender
If e.Row Is rowTotal And e.IsGetData Then
Dim row As DataRowView = vGrid.GetRecordObject(e.ListSourceRowIndex)
Dim unitPrice As Decimal = Convert.ToDecimal(row("UnitPrice"))
Dim quantity As Decimal = Convert.ToDecimal(row("Quantity"))
Dim discount As Decimal = Convert.ToDecimal(row("Discount"))
e.Value = unitPrice * quantity * (1 - discount)
End If
End Sub
See Also