Back to Devexpress

VGridControl.CustomUnboundData Event

windowsforms-devexpress-dot-xtraverticalgrid-dot-vgridcontrol-719a9139.md

latest10.8 KB
Original Source

VGridControl.CustomUnboundData Event

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

Declaration

csharp
public event CustomDataEventHandler CustomUnboundData
vb
Public Event CustomUnboundData As CustomDataEventHandler

Event Data

The CustomUnboundData event's data class is CustomDataEventArgs. The following properties provide information specific to this event:

PropertyDescription
IsGetDataGets whether the column/row needs to be populated with cell values. Inherited from UnboundColumnDataEventArgs.
IsSetDataGets whether a cell value changed and data needs to be posted to a data source. Inherited from UnboundColumnDataEventArgs.
ListSourceRowIndexGets the column’s index in the data source.
RecordObjectReturns an object in the bound data source that contains data for the processed column.
RowGets the unbound row.
RowPropertiesGets a row that contains a cell currently being processed.
ValueGets or sets a cell value in an unbound column. Inherited from UnboundColumnDataEventArgs.
ValueTypeGets the value of the UnboundDataType property.

Remarks

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:

  • When the grid is loaded it raises the CustomUnboundData event to populate the unbound rows. The event’s IsGetData parameter will be set to true (consequently the IsSetData parameter will be set to false ). In this case, you need to supply data for the currently processed cell. Obtain the required value from a custom data source and assign it to the Value parameter.
  • When an unbound row’s data is modified via the grid, the CustomUnboundData event is fired with the IsSetData parameter set to true (consequently the IsGetData parameter is set to false ). In this case, you should save the modified data specified by the Value property back to the custom data source.

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).

Example 1

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.

csharp
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();
    }
}
vb
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

Example 2

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).

csharp
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);
    }
}
vb
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

UnboundDataType

Unbound Rows

VGridControl Class

VGridControl Members

DevExpress.XtraVerticalGrid Namespace