Back to Devexpress

Moving Row Focus

windowsforms-1028-controls-and-libraries-data-grid-focus-and-selection-handling-moving-row-focus.md

latest10.4 KB
Original Source

Moving Row Focus

  • Oct 08, 2024
  • 5 minutes to read

This topic explains how to do the following:

  • Get/set the focused row.

  • Get/set a cell value in the focused row.

  • Move row focus by a specified number of rows.

  • Hide row focus.

  • Change the focused row’s appearance.

Focus and Selection

When a row receives focus, a user can interact with its cells (for example, edit or copy their values). Only one row in a view can be focused at a time, while multiple rows can be selected simultaneously if multi-select mode is enabled.

A user can focus a row as follows:

A developer can also focus a row programmatically.

Get or Set the Focused Row

APIDescription
ColumnView.FocusedRowHandleRead this property’s value to retrieve the currently focused row’s handle. Modify the property value to focus a specific row.

The ColumnView.FocusedRowChanged event fires each time the FocusedRowHandle value changes.

csharp
// Focus the second data row in the view
gridView1.FocusedRowHandle = 1;
// Focus the first group row in the view
gridView1.FocusedRowHandle = -1;
vb
' Focus the second data row in the view
gridView1.FocusedRowHandle = 1
' Focus the first group row in the view
gridView1.FocusedRowHandle = -1

Note

Read the following help topic for information on how to identify rows (data row, group row, new item row, etc.): Tutorial - Identifying Rows.

Get ot Set a Cell Value in the Focused Row

APIDescription
ColumnView.GetFocusedRowCellValue(GridColumn)Returns the specified column’s value within the focused row. Returns null if the column is not found.
SetFocusedRowCellValueAssigns a value to the specified column within the currently focused row. This method has an overload that accepts a column’s field name as a string.

Move Row Focus by a Specified Number of Rows

MethodDescription
ColumnView.MoveFirstMoves focus to the first visible row within the View.
ColumnView.MoveNextMoves focus to the next row.
ColumnView.MovePrevMoves focus to the previous row.
ColumnView.MoveNextPageMoves focus forward by the number of rows displayed within the View.
ColumnView.MovePrevPageMoves focus backward by the number of rows displayed within the View.
ColumnView.MoveLastVisibleMoves focus to the last visible row.
ColumnView.MoveByMoves focus by the specified number of rows.
ColumnView.MoveLastMoves focus to the last row within the View.

Tip

The Data Grid ships an embedded Data Navigator component for navigation between rows.

Focused Row Appearance

Appearance Settings

Use the GridViewAppearances.FocusedRow property to customize the appearance of the focused row.

The following code sets the background to red and the text color to white for the focused row:

csharp
using DevExpress.XtraGrid.Views.Grid;

void Form1_Load(object sender, EventArgs e) {
  gridView1.Appearance.FocusedRow.BackColor = Color.Red;
  gridView1.Appearance.FocusedRow.ForeColor = Color.White;
}
vb
Imports DevExpress.XtraGrid.Views.Grid

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
  gridView1.Appearance.FocusedRow.BackColor = Color.Red
  gridView1.Appearance.FocusedRow.ForeColor = Color.White
End Sub

The following image shows the result:

Tip

By default, the focused cell visually differs from other cells in the focused row. Disable the GridOptionsSelection.EnableAppearanceFocusedCell property to apply the same appearance settings to all cells in the focused row.

Custom Appearance

The following help topics describe advanced customization scenarios:

Disable Focused Row Appearance

Use the EnableAppearanceFocusedRow property to disable appearance settings that paint the focused row.

The following example disables special appearance settings for the focused row, hides the focus rectangle (FocusRectStyle ), and hides the row indicator (ShowIndicator ):

csharp
using DevExpress.XtraGrid.Views.Grid;

// Disable appearance settings that paint the focused row.
gridView1.OptionsSelection.EnableAppearanceFocusedRow = false;
// Hide the focused rectangle.
gridView1.FocusRectStyle = DrawFocusRectStyle.None;
// Hide the row indicator.
gridView1.OptionsView.ShowIndicator = false;
vb
Imports DevExpress.XtraGrid.Views.Grid

' Disable appearance settings that paint the focused row.
gridView1.OptionsSelection.EnableAppearanceFocusedRow = False
' Hide the focused rectangle.
gridView1.FocusRectStyle = DrawFocusRectStyle.None
' Hide the row indicator.
gridView1.OptionsView.ShowIndicator = False

The following image shows the result (the first row is the focused row):

Hide Row Focus (Temporarily)

MethodDescription
ColumnView.FocusInvalidRowTemporarily hides row focus.

The grid control always focuses a valid row when it receives focus. The default valid row is the first row.

The following example temporarily hides the focused row at application startup:

csharp
void Form1_Shown(object sender, EventArgs e) {
  gridView1.FocusInvalidRow();
}
vb
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs)
    gridView1.FocusInvalidRow()
End Sub

The following image shows the result. Once a user clicks a row, the row will receive focus again.

APIDescription
ColumnView.IsFirstRowReturns true if the focused row is the first row within a View.
ColumnView.IsLastRowReturns true if the focused row is the last row within a View.
ColumnView.IsLastVisibleRowReturns true if the focused row is the last visible row within a View.
BaseView.RowCountReturns the total number of visible rows within a View.
BaseView.DataRowCountReturns the number of data rows within a View. Compare this value to the ColumnView.FocusedRowHandle property to determine whether the currently focused row is the last row within a View.

See Also

Rows

Focus Data Grid Cells

Data Row

Tutorial: Identifying Rows

Multiple Row and Cell Selection

Appearance and Conditional Formatting