Set focus inside DataGrid

wpf datagrid scrollintoview
wpf datagrid cell focus event
datagrid keyboard focus
wpf datagrid arrow key navigation
wpf datagrid select row programmatically
wpf datagrid add row programmatically
datagrid move focus
datagrid select item

I Have a DataGrid with some data loaded. I need to set focus in the first row of this grid.

I will call this method from a button onClick event.

The myGrid.Focus() does not focus the rows inside the grid.

It is one of the hardest job in WPF, and reason is because of Virtualization and the fact that UI Render thread is different from the thread that runs our code (you can not find when exactly UI render finished). for a complete reference you can have look here

using Dispatcher.Invoke might works for some situations (believe me in WPF Dispatcher.Invoke is your best friend and biggest enemy)

dgGrid.ItemsSource = new List<object>() { new { I = 10, J = 20 }, new { I = 10, J = 20 }, new { I = 10, J = 20 }, new { I = 10, J = 20 }, new { I = 10, J = 20 } };
        Dispatcher.Invoke(new Action(delegate()
            grd.SelectedIndex = 0;
    ), System.Windows.Threading.DispatcherPriority.Background);

or robustest one from linked article

public static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
    if (rowContainer != null)
        DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
        if (presenter == null)
            /* if the row has been virtualized away, call its ApplyTemplate() method
             * to build its visual tree in order for the DataGridCellsPresenter
             * and the DataGridCells to be created */
            presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
        if (presenter != null)
            DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
            if (cell == null)
                /* bring the column into view
                 * in case it has been virtualized away */
                dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
                cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
            return cell;
    return null;

  public static void SelectRowByIndex(DataGrid dataGrid, int rowIndex)
        if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow))
            throw new ArgumentException("The SelectionUnit of the DataGrid must be set to FullRow.");

        if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1))
            throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));

        /* set the SelectedItem property */
        object item = dataGrid.Items[rowIndex]; // = Product X
        dataGrid.SelectedItem = item;

        DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
        if (row == null)
            /* bring the data item (Product object) into view
             * in case it has been virtualized away */
            row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
         if (row != null)
        DataGridCell cell = GetCell(dataGrid, row, 0);
        if(cell != null)

you need to add these static methods call the second one it first tries tries find (or draw the row if it is draw yet then set focus on it).

Wpf datagrid adding new row setting focus first cell, Check this: c# - how to set focus to particular cell of WPF toolkit datagrid[^]. I Have a DataGrid with some data loaded. I need to set focus in the first row of this grid. I will call this method from a button onClick event. The myGrid.Focus() does not focus the rows inside the grid.

Try this:

if (e.Key == Key.Up || e.Key == Key.Down) 
    var uiElement = e.OriginalSource as UIElement; 
    dgvRoute.Focus(); //Datagrid 
    uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

WPF datagrid focus on cell, b) set the cursor focus to the first visible cell in this row. Saturday, August 27, 2011 2:35 PM. Avatar of  The row details section can also be frozen so that it does not scroll horizontally when the DataGrid is scrolled. To display a row details section using inline XAML. Create a DataTemplate that defines the appearance of the row details section. Place the DataTemplate inside the <DataGrid.RowDetailsTemplate> tags.

mygrid.SelectedItem = mygrid.Items.Count > 0 ? mygrid.Items[0] : null;

Datagrid set focus to first cell in the add row - MSDN, Hi, support team. Please, is there a way to set focus to the first column and turn it editable , after I've inserted another line in the data grid? There is a lot of questions out there on how to focus on a particular cell in a WPF application. If you have the data in you datagrid updated from somewhere else, maybe from SignalR or just updating your Observable object from somewhere in your application, and you want to focus that particular row and…

If you want to focus first value in grid try following

myGrid.SelectedIndex = 0;

T693859 - DataGrid, The grid is scrolled down to it. If you also set the focusedColumnIndex option, a specific cell in this row is focused. focus the first cell -->; </dx-data-grid>. When setting initial focus at application startup, the element to receive focus must be in the visual tree of the initial window loaded by the application, and the element must have Focusable and IsVisible set to true. The recommended place to set initial focus is in the Loaded event handler.

Focused Row: DevExtreme, tried to select a row in a DataGrid in WPF programmatically by setting set any of the mentioned properties in code the row will lose its focus. How to: Host Controls in Windows Forms DataGridView Cells. 03/30/2017; 7 minutes to read +9; In this article. The DataGridView control provides several column types, enabling your users to enter and edit values in a variety of ways. If these column types do not meet your data-entry needs, however, you can create your own column types with cells

How to programmatically select and focus a row or cell in a DataGrid , You need to subscribe to the AddingNewDataItem event of the RadGridView and set the current column in its event handler: C#; VB.NET.

WPF DataGrid | Always focus the first cell on insert, In case of a textbox, I'd also like the existing contents to be selected by default ; so that i can begin typing and set a new value quickly. This works for the default 

  • It works! And it is the simplest I've seen!
  • One sentence answers are not appreciated on StackOverflow. Please see the How do I write a good answer? page from the Help Center for more information.
  • This does not focus on the grid. Actually, we need to focus on the cell, like example above.