Back to Devexpress

Focus and Scroll Rows

windowsforms-483-controls-and-libraries-vertical-grid-data-layout-records-rows-and-cells-rows-focus-and-scroll-rows.md

latest4.8 KB
Original Source

Focus and Scroll Rows

  • Sep 03, 2021
  • 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:

The following sample code focuses the rowPrice row if it is visible. Otherwise, the first visible row is focused.

csharp
if (rowPrice.Visible)
   vGridControl1.FocusedRow = rowPrice;
else 
   vGridControl1.FocusFirst();
vb
If rowPrice.Visible Then
   VGridControl1.FocusedRow = rowPrice
Else
   VGridControl1.FocusFirst()
End If

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.

csharp
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();
}
vb
Private Sub VGridControl1_HiddenEditor(ByVal sender As Object, ByVal e As System.EventArgs) Handles VGridControl1.HiddenEditor
   Dim vGrid As VGridControl = sender
   MoveNext(vGrid)
   While ((TypeOf (vGrid.FocusedRow) Is CategoryRow) Or Not vGrid.CanShowEditor)
      MoveNext(vGrid)
   End While
   vGrid.ShowEditor()
End Sub
' Moves focus to the next row or to the first row when the last one has been achieved.
Private Sub MoveNext(ByVal Grid As VGridControl)
   If Grid.FocusedRow Is Grid.GetLastVisible() Then
      Grid.FocusFirst()
      Grid.FocusedRecord += 1
   Else
      Grid.FocusNext()
   End If
End Sub

Scroll Rows in Code

Use the following API to scroll records in code: