Skip to main content

RowProperties.RowHandle Property

Gets the position of the associated field within a data source.

Namespace: DevExpress.XtraVerticalGrid.Rows

Assembly: DevExpress.XtraVerticalGrid.v23.2.dll

NuGet Packages: DevExpress.Win.Navigation, DevExpress.Win.VerticalGrid

Declaration

[Browsable(false)]
public virtual int RowHandle { get; }

Property Value

Type Description
Int32

An integer value representing the zero-based index of the corresponding field within a data source.

Remarks

When a vertical grid control is in bound mode, it can display field values from the associated data source with the help of row items. Row items can be bound to data source fields via the RowProperties.FieldName property. If a row item is bound to a field, the RowHandle property returns the index of the bound field within the associated data source, otherwise it returns -1.

When working in Unbound Mode, the VGridControl.DataSource property is set to null (Nothing in Visual Basic). Hence, row items are not bound to any field. In this case, the RowHandle property of any row item returns -1.

You can use specific overloaded variants of the VGridControlBase.GetCellValue and VGridControlBase.SetCellValue methods to correspondingly obtain or set the value of the data cell specified by its row handle and the corresponding record’s index.

Example

The following example demonstrates how to use the RowProperties.RowHandle property to organize grid rows into the sequence order of their associated data fields. This example assumes that the following conditions should or may take place:

  • The grid is created and bound to a data source.
  • Grid rows are linked to certain data fields.
  • Initial row structure may not follow the order of fields in the data source.
  • The grid may contain several multi-editor rows.
  • Each editor row (or item in a multi-editor row) may be associated with a particular editor used to display and edit its data values.

This example visits each grid row and performs an operation represented by the RowOperationRestoreOrder class instance. This class is the RowOperation class descendant. Row operations implemeted by this class hide all category rows to the Customization Form, separate multi-editor rows (if any) into individual editor rows and move all rows to the top level.

Finally, rows in the VGridControlBase.Rows collection are reordered to follow the order of associated fields in the linked data source.

The image below shows how rows are organized in the grid control before and after code execution.

RowProperties_RowHandle

// implementing RowOperationRestoreOrder class
public class RowOperationRestoreOrder : RowOperation {
    private VGridControl vGrid;
    // reserving a list for category and editor rows processing
    private ArrayList childRows; 
    // reserving a list for multi-editor rows processing
    private ArrayList meRows; 

    public RowOperationRestoreOrder(VGridControl grid) : base() {
        this.vGrid = grid;
    }
    public override void Init(){
        // creating a specific list for child category and editor rows
        childRows = new ArrayList(); 
        // creating a specific list for multi-editor rows
        meRows = new ArrayList(); 
        vGrid.BeginUpdate(); // preventing the grid from being updated
    }
    public override void Execute(DevExpress.XtraVerticalGrid.Rows.BaseRow row){
        switch (row.XtraRowTypeID){
            case 0: // category row is processed
                // adding non top-level category row to specific list
                if (row.ParentRow != null) childRows.Add(row); 
                // hiding row in the Customization Form
                row.Visible = false; 
                break;
            case 1: // editor row is processed
                // adding non top-level editor row to specific list
                if (row.ParentRow != null) childRows.Add(row); 
                // making row visible
                if (!row.Visible) row.Visible = true; 
                break;
            case 2: // multi-editor row is processed
                // adding multi-editor row to specific list
                meRows.Add(row); 
                break;
        }
    }

    public override void Release(){
        // splitting each multi-editor row from the obtained list into several editor rows
        foreach (MultiEditorRow meRow in meRows) {
            foreach (RowProperties rowItem in meRow.PropertiesCollection) {
                // creating a new editor row for each row item 
                // of the processed multi-editor row
                EditorRow eRow = new EditorRow(); 
                // copying property settings from row item to the new editor row
                rowItem.AssignTo(eRow.Properties); 
                // adding the newly created row to the grid collection of top-level rows
                vGrid.Rows.Add(eRow); 
            }
            // deleting the processed multi-editor row
            if (meRow.ParentRow != null) meRow.ParentRow.ChildRows.Remove(meRow);
            else meRow.Grid.Rows.Remove(meRow);
        }
        // moving each category or editor row from the obtained list 
        // to the collection of top-level grid rows
        foreach (BaseRow row in childRows) {
            row.Grid.MoveRow(row, row.Grid.Rows[0], true);
        }
        // unlocking the grid and causing its immediate repainting
        vGrid.EndUpdate(); 
    }
}

private void RestoreRowOrder() {
    // creating an instance of the RowOperationRestoreOrder class 
    // and performing the operation on rows
    RowOperationRestoreOrder restoreOrder = new RowOperationRestoreOrder(vGridControl1);
    vGridControl1.RowsIterator.DoOperation(restoreOrder);

    // creating an instance of a list used to automatically sort grid rows 
    // according to associated data fields
    SortedList sortList = new SortedList();
    // adding RowHandle and Name property values of all non-category rows 
    // to list as key-value pairs
    foreach (BaseRow row in vGridControl1.Rows) {
        if (row.Properties.RowHandle != -1) 
            sortList.Add(row.Properties.RowHandle, row.Name);
    }
    // setting the list capacity to the actual number of elements
    sortList.TrimToSize();
    // bringing order of grid rows in correspondence with the data fields order 
    for (int i = 0; i < sortList.Count; i++ )  {
        vGridControl1.Rows[sortList.GetByIndex(i).ToString()].Index = i;
    }
}
See Also