Back to Devexpress

Focus Data Grid Cells

windowsforms-741-controls-and-libraries-data-grid-focus-and-selection-handling-focusing-cells.md

latest12.3 KB
Original Source

Focus Data Grid Cells

  • Oct 04, 2024
  • 5 minutes to read

This help topic explains how to do the following:

  • Focus cells in code.

  • Get or set the focused cell’s value/display text.

  • Change the focused cell’s appearance.

Focus and Selection

When a cell receives focus, a user can interact with it (for example, edit or copy its value). Only one cell in a view can be focused at a time, while multiple cells can be selected simultaneously if multi-select mode is enabled. Appearance settings for focused cells are not applied to selected cells.

In the following image, the leftmost cell in the first row is focused.

Note

When the grid control shows master-detail data, it displays several Views. Each View has its own focused cell. The grid control’s GridControl.FocusedView property determines the view whose focused cell will respond to key presses.

How a Cell Receives Focus

A user can focus a cell as follows:

A developer can also focus a cell programmatically.

Focus Cells in Code

To identify the cell you want to focus, identify its row and column. Read the following help topics for more information:

To get or set the View’s focused cell, use the following properties:

  • ColumnView.FocusedRowHandle. If the specified data row is not currently visible on the screen, this property scrolls the View and expands collapsed groups to make the data row visible.

  • ColumnView.FocusedColumn. If the specified column is not currently visible on the screen, this property scrolls the View horizontally to make the column visible.

The following example implements two methods that allow you to focus a specific cell. The FocusCellByRowHandle method focuses a cell in a data row based on a column and a row handle. The FocusCellByVisibleIndex method focuses a cell in a data row based on a column and a row’s visible index.

csharp
void FocusCellByRowHandle(ColumnView view, int rowHandle, GridColumn column) {
    if (view == null || !view.IsDataRow(rowHandle)) return;
    view.FocusedColumn = column;
    view.FocusedRowHandle = rowHandle;
}
void FocusCellByVisibleIndex(ColumnView view, int visibleIndex, GridColumn column) {
    if (view == null) return;
    view.FocusedColumn = column;
    int rowHandle = view.GetVisibleRowHandle(visibleIndex);
    if (!view.IsDataRow(rowHandle)) return;
    view.FocusedRowHandle = rowHandle;

}
vb
Private Sub FocusCellByRowHandle(ByVal view As ColumnView, ByVal rowHandle As Integer, ByVal column As GridColumn)
    If view Is Nothing OrElse Not view.IsDataRow(rowHandle) Then
        Return
    End If
    view.FocusedColumn = column
    view.FocusedRowHandle = rowHandle
End Sub
Private Sub FocusCellByVisibleIndex(ByVal view As ColumnView, ByVal visibleIndex As Integer, ByVal column As GridColumn)
    If view Is Nothing Then
        Return
    End If
    view.FocusedColumn = column
    Dim rowHandle As Integer = view.GetVisibleRowHandle(visibleIndex)
    If Not view.IsDataRow(rowHandle) Then
        Return
    End If
    view.FocusedRowHandle = rowHandle

End Sub

Get or Set the Focused Cell’s Value

|

API

|

Description

| | --- | --- | |

ColumnView.GetFocusedValue

|

Returns the focused cell’s value.

| |

ColumnView.FocusedValue

|

Gets the focused cell value.

| |

ColumnView.SetFocusedValue

|

Assigns a value to the focused cell.

|

The following code increments the value of the focused cell in the “Quantity” column by 5 on a button click:

csharp
void simpleButton1_Click(object sender, EventArgs e) {
    if (gridView1.FocusedColumn.FieldName == "Quantity") {
        gridView1.SetFocusedValue((int)gridView1.FocusedValue + 5);
    }
}
vb
Sub simpleButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
    If gridView1.FocusedColumn.FieldName = "Quantity" Then
        gridView1.SetFocusedValue(CInt(Math.Truncate(gridView1.FocusedValue)) + 5)
    End If
End Sub

Tip

See the following help section for more information: Get and Modify Cell Values in Code.

Get the Focused Cell’s Display Text

|

API

|

Description

| | --- | --- | |

ColumnView.GetFocusedDisplayText

|

Returns the focused cell’s display value.

|

Tip

The following help section explains the difference between a cell’s value and a cell’s display text: Display Text and Cell Value.

Change the Focused Cell’s Appearance

Appearance Settings

Use the GridViewAppearances.FocusedCell property to specify appearance settings of the focused cell. This property has priority over other appearance settings unless these settings have AppearanceOptionsEx.HighPriority enabled.

The following code sets a red background and a blue text color for the focused cell:

csharp
void Form1_Load(object sender, EventArgs e) {
    gridView1.Appearance.FocusedCell.BackColor = Color.Red;
    gridView1.Appearance.FocusedCell.ForeColor = Color.Blue;
}
vb
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    gridView1.Appearance.FocusedCell.BackColor = Color.Red
    gridView1.Appearance.FocusedCell.ForeColor = Color.Blue
End Sub

The following image shows the result:

Focus Rectangle

Use the GridView.FocusRectStyle property to change the way the focus rectangle is painted.

The following code hides the focus rectangle:

csharp
using DevExpress.XtraGrid.Views.Grid;
void Form1_Load(object sender, EventArgs e) {
    gridView1.FocusRectStyle = DrawFocusRectStyle.None;
}
vb
Imports DevExpress.XtraGrid.Views.Grid
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    gridView1.FocusRectStyle = DrawFocusRectStyle.None
End Sub

The following image shows the result:

Watch Video: Draw a Border Around the Focused Cell

Change the Active Cell Editor’s Appearance

Handle the ColumnView.ShownEditor event to configure the appearance of the cell when a user edits its value.

csharp
void gridView1_ShownEditor(object sender, EventArgs e) {
    gridView1.ActiveEditor.BackColor = Color.Blue;
    gridView1.ActiveEditor.ForeColor = Color.White;
}
vb
Private Sub gridView1_ShownEditor(ByVal sender As Object, ByVal e As EventArgs)
    gridView1.ActiveEditor.BackColor = Color.Blue
    gridView1.ActiveEditor.ForeColor = Color.White
End Sub

The following image shows the result:

Tip

Read the following help section for information on cell editors: Cell Editors.

Custom Appearance

The following help topics describe advanced customization scenarios:

Events

|

API

|

Description

| | --- | --- | |

GridControl.FocusedViewChanged

|

Fires in response to focus moving between Views.

| |

ColumnView.FocusedRowChanged

|

Fires when row focus moves from one row to another.

| |

ColumnView.FocusedColumnChanged

|

Fires in response to changing column focus.

| |

GridView.RowCellStyle

|

Fires for every visible Grid cell before this cell is shown. Allows you to modify Appearance settings for this cell.

|

Properties

|

API

|

Description

| | --- | --- | |

OptionsColumn.AllowFocus

|

Gets or sets whether end users can move focus to the column using the mouse or keyboard.

| |

GridViewAppearances.FocusedRow

|

Gets appearance settings used to paint the currently focused row.

| |

GridOptionsSelection.InvertSelection

|

Gets or sets a value specifying whether the focused style is applied to the focused cell only or to all cells in the focused row except for the focused cell.

| |

GridOptionsSelection.EnableAppearanceFocusedCell

|

Gets or sets whether the appearance settings for the focused cell are enabled.

|

See Also

End-User Capabilities: Navigating Through Rows and Cells

Moving Row Focus

Appearance and Conditional Formatting

Edit Data. Create Cell Editors. Validate User Input

Using Navigators