Focus and Scroll Rows
- 3 minutes to read
The vertical and property grid controls allow you to navigate through rows in code. Typical scenarios:
- focus a specific row;
- scroll to a specific row without focusing it;
- ensure that multiple rows are visible simultaneously;
- display a specific row at the top of the grid;
- synchronize the grid with a control that displays related data.
This topic describes how to focus rows and scroll the view vertically. To learn how to focus records and scroll the view horizontally, see Focus and Scroll Records. To learn how users can focus cells and scroll the view, see Navigating Through Cells.
Focus a Row in Code
Use the following API to focus records in code:
- VGridControl.FocusedRow — gets or sets the currently focused row. See Rows for information on how to access rows in code.
- VGridControlBase.FocusFirst, VGridControlBase.FocusLast — focus the first and last row, respectively.
- VGridControl.FocusNext, VGridControlBase.FocusPrev — focus the next and previous rows, respectively. These methods automatically expand collapsed child rows.
- EditorRow.OptionsRow.AllowFocus — gets or sets whether users can focus the row.
- VGridControl.FocusedRowChanged — fires when focus changes.
The following sample code focuses the rowPrice row if it is visible. Otherwise, the first visible row is focused.
if (rowPrice.Visible)
vGridControl1.FocusedRow = rowPrice;
else
vGridControl1.FocusFirst();
The example below shows how to automatically invoke the next cell’s editor when the current cell is completed. When the last row is edited, focus moves to the next record.
private void vGridControl1_HiddenEditor(object sender, System.EventArgs e) {
VGridControl vGrid = sender as VGridControl;
MoveNext(vGrid);
while ((vGrid.FocusedRow is CategoryRow) || !vGrid.CanShowEditor)
MoveNext(vGrid);
vGrid.ShowEditor();
}
// Moves focus to the next row or to the first row when the last one has been achieved.
private void MoveNext(VGridControl grid) {
if (grid.FocusedRow == grid.GetLastVisible()) {
grid.FocusFirst();
grid.FocusedRecord++;
}
else
grid.FocusNext();
}
Scroll Rows in Code
Use the following API to scroll records in code:
- VGridControlBase.ScrollVisibility — gets or sets whether the vertical scroll bar is visible.
VGridControlBase.MakeRowVisible — scrolls the view up or down up to the specified row and expands its parent rows if required.
VGridControlBase.TopVisibleRowIndex — gets or sets the zero-based index of the top visible row.
VGridControlBase.VertScroll — scrolls the view by up or down the specified number of rows.
Note
The VGridControlBase.TopVisibleRowIndex property and the VGridControlBase.VertScroll method do not automatically expand the specified row if it is collapsed. Use the VGridControlBase.MakeRowVisible method for this purpose.