windowsforms-741-controls-and-libraries-data-grid-focus-and-selection-handling-focusing-cells.md
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.
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.
A user can focus a cell as follows:
Click the cell.
Use the keyboard to navigate to the cell. Read the following help topic for more information: Navigating Through Rows and Cells.
A developer can also focus a cell programmatically.
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.
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;
}
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
|
API
|
Description
| | --- | --- | |
|
Returns the focused cell’s value.
| |
|
Gets the focused cell value.
| |
|
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:
void simpleButton1_Click(object sender, EventArgs e) {
if (gridView1.FocusedColumn.FieldName == "Quantity") {
gridView1.SetFocusedValue((int)gridView1.FocusedValue + 5);
}
}
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.
|
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.
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:
void Form1_Load(object sender, EventArgs e) {
gridView1.Appearance.FocusedCell.BackColor = Color.Red;
gridView1.Appearance.FocusedCell.ForeColor = Color.Blue;
}
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:
Use the GridView.FocusRectStyle property to change the way the focus rectangle is painted.
The following code hides the focus rectangle:
using DevExpress.XtraGrid.Views.Grid;
void Form1_Load(object sender, EventArgs e) {
gridView1.FocusRectStyle = DrawFocusRectStyle.None;
}
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
Handle the ColumnView.ShownEditor event to configure the appearance of the cell when a user edits its value.
void gridView1_ShownEditor(object sender, EventArgs e) {
gridView1.ActiveEditor.BackColor = Color.Blue;
gridView1.ActiveEditor.ForeColor = Color.White;
}
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.
The following help topics describe advanced customization scenarios:
|
API
|
Description
| | --- | --- | |
GridControl.FocusedViewChanged
|
Fires in response to focus moving between Views.
| |
|
Fires when row focus moves from one row to another.
| |
ColumnView.FocusedColumnChanged
|
Fires in response to changing column focus.
| |
|
Fires for every visible Grid cell before this cell is shown. Allows you to modify Appearance settings for this cell.
|
|
API
|
Description
| | --- | --- | |
|
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
Appearance and Conditional Formatting