Focus
- 4 minutes to read
Moving the cell focus in code may be useful when you need to facilitate end user input. This enables you to move focus to the desired cells automatically, so end users don’t need to navigate themselves. This topic describes the basics of focus movement between cells.
Focusing Cells
Availability
Data cells can be focused if the DataViewBase.NavigationStyle property is set to GridViewNavigationStyle.Cell.
Focusing Cells
The focused cell is a data cell with which an end-user can interact using the keyboard. For instance, an end-user can press F2 or ENTER to activate the cell’s in-place editor.
The focused cell is identified by the focused row and focused column. The focused row is specified by the DataViewBase.FocusedRowHandle property. The focused column is specified by the DataControlBase.CurrentColumn property.
You can use the following methods to move cell focus within the focused row:
Method | Description |
---|---|
DataViewBase.MoveNextCell | Focuses the next cell after the focused cell. |
DataViewBase.MovePrevCell | Focuses the previous cell before the focused cell. |
Example: How to Focus a Cell with the Specified Value
This example shows how to identify a data cell with the specified value and focus it. To do this, click the 'Find Next' button. The search is performed starting from the currently focused row.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-focus-a-cell-with-the-specified-value-e1544.
<Window x:Class="DXSample_FocusingCells.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="Focusing Cells" Height="300" Width="491">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="32" />
</Grid.RowDefinitions>
<dxg:GridControl Margin="5" Name="grid" AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView x:Name="view" AutoWidth="True" ShowGroupPanel="False" AllowGrouping="False" NavigationStyle="Cell" />
</dxg:GridControl.View>
</dxg:GridControl>
<Button Grid.Row="1" HorizontalAlignment="Left" Margin="2" Click="Button_Click">
Find Next</Button>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.Grid;
namespace DXSample_FocusingCells {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
grid.ItemsSource = new nwindDataSetTableAdapters.ProductsTableAdapter().GetData();
}
private void Button_Click(object sender, RoutedEventArgs e) {
if (grid.VisibleRowCount == 0) return;
int rowHandle = view.FocusedRowHandle + 1;
while (Convert.ToDouble(grid.GetCellValue(rowHandle, "UnitPrice")) != 10 &&
grid.IsValidRowHandle(rowHandle)) {
rowHandle++;
}
if (grid.IsValidRowHandle(rowHandle)) {
grid.CurrentColumn = grid.Columns["UnitPrice"];
view.FocusedRowHandle = rowHandle;
}
}
}
}
Focusing Rows
Availability
Rows can be focused if the DataViewBase.NavigationStyle property isn’t set to GridViewNavigationStyle.None. To specify the focused row, use the DataViewBase.FocusedRowHandle property. If the specified row isn’t visible onscreen, a View is automatically scrolled to make the focused row visible.
If the row focusing is enabled, the GridControl focuses the first row when loaded by default. To load the GridControl without focused rows, set the DataControlBase.AllowInitiallyFocusedRow property to false.
Moving Row Focus
The focused row is specified by its handle using the DataViewBase.FocusedRowHandle property. The GridControl provides multiple members that you can use to move row focus:
Member | Description |
---|---|
DataControlBase.CurrentItem | Gets or sets the currently focused data row. |
DataViewBase.MoveFirstRow | Moves focus to the first visible row or card within a View. |
DataViewBase.MovePrevPage | Moves focus backward by the number of rows or cards displayed onscreen within a View. |
DataViewBase.MovePrevRow | Moves focus to the row or card preceding the one currently focused. |
DataViewBase.MoveNextRow | Moves focus to the row or card following the one currently focused. |
DataViewBase.MoveNextPage | Moves focus forward by the number of rows or cards displayed onscreen within a View. |
DataViewBase.MoveLastRow | Moves focus to the last visible row or card within a View. |
DataViewBase.MoveFocusedRow | Moves focus to the specified row. |
GridViewBase.MoveParentGroupRow | Moves focus to the group row that owns the currently focused row. |
All these methods move focus between visible rows. These methods don’t expand collapsed groups.
After the focused row has been changed, the DataControlBase.CurrentItemChanged event is raised.
The number of visible rows is returned by the DataControlBase.VisibleRowCount property. Note that rows contained within collapsed group rows are not taken into account.
Set the DataViewBase.FadeSelectionOnLostFocus property to false to keep the focused row highlighted when the GridControl loses focus.