Focusing Cells

  • 4 minutes to read

This topic provides information on focus movement between cells at runtime, as well as information on how to specify a focused cell's appearance. Moving the cell focus at runtime may be useful when you need to facilitate end-user input. This enables you to move focus to desired cells automatically so end-users don't need to navigate through the Views themselves. This topic only describes the basics of focus movement between cells. For additional information on this subject, please refer to the Moving Row Focus topic.

Focusing Cells

The focused cell is a data cell with which the end-user can interact using the keyboard. For instance, end-users can press the F2 key to activate a cell's editor. Note: the grid control may represent master-detail data and so display several Views each of which have its own focused cell. The view, whose focused cell will actually respond to keystrokes, is determined by the grid control's GridControl.FocusedView property.

A View's focused cell is identified by its focused row and focused column. These are specified by the View's ColumnView.FocusedRowHandle and ColumnView.FocusedColumn properties respectively. Please refer to the Identifying Rows and Cards and Accessing and Identifying Columns topics for details on providing values for these properties.

Assigning a row handle to a View's ColumnView.FocusedRowHandle property scrolls the View and expands collapsed groups so that the specified row is visible on screen. This occurs also when you focus rows using other members (for instance, when using methods described in the Moving Row Focus topic). Similarly, when setting the ColumnView.FocusedColumn property to a column that is currently not visible due to scrolling, the View is scrolled horizontally to make the column visible. The ColumnView.FocusedColumn property can also accept hidden columns (whose GridColumn.Visible property value is false). In such cases, the specified column will be focused but remain invisible.

The following example shows how to locate and focus a cell in the Country column that contains 'Japan'. Note that the row is located using the ColumnView.LocateByDisplayText method. Please refer to the Filter and Search topic for details on using it.


using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
// ...

string searchText = "Japan";
// obtaining the focused view
ColumnView View = (ColumnView)gridControl1.FocusedView;
// obtaining the column bound to the Country field
GridColumn column = View.Columns["Country"];
if(column != null) {
    // locating the row
    int rhFound = View.LocateByDisplayText(View.FocusedRowHandle + 1, column, searchText);
    // focusing the cell
    if(rhFound != GridControl.InvalidRowHandle) {
        View.FocusedRowHandle = rhFound;
        View.FocusedColumn = column;
    }
}

You can handle focus movements by writing handlers for the GridControl.FocusedViewChanged, ColumnView.FocusedRowChanged and ColumnView.FocusedColumnChanged events. These events are raised when one is moving focus to another view, row and column respectively. Note also that you can prevent particular columns from being focused by end-users. Disable the desired column's OptionsColumn.AllowFocus option for this purpose.

For information on how end-users can change cell focus, refer to the Navigating Through Rows and Cells topic.

The Focused Cell's Appearance

By default, the appearance of the focused cell is specified by the GridViewAppearances.FocusedCell property.The appearance of other cells within the focused row is specified by the GridViewAppearances.FocusedRow property.

Note that you can change this default behavior by setting the GridOptionsSelection.InvertSelection property to true. In this case, the focused cell will be painted using the FocusedRow and FocusedCell appearances when browsing and editing respectively. Other cells within the focused row will have the same appearance as cells within unfocused rows.

The dotted focus rectangle can be painted around the focused cell, the entire focused row or not be displayed at all by using the GridView.FocusRectStyle property.

Online Video - WinForms Data Grid: Draw a Border Around the Focused Cell

See Also