Skip to main content

VGridControl.CustomUnboundData Event

Enables data to be provided to and modified data to be saved from unbound rows.

Namespace: DevExpress.XtraVerticalGrid

Assembly: DevExpress.XtraVerticalGrid.v24.2.dll

Declaration

public event CustomDataEventHandler CustomUnboundData

Event Data

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.

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.

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

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

VGridControl_UnboundRow_ex

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);
    }
}
See Also