9

I would like to set the focus on the first row of a data grid.

This is what I have so far:

Keyboard.Focus(ResultsGrid)
If result.Count > 0 Then
    ResultsGrid.SelectedIndex = 0
End If

This will set the focus to the datagrid, but not the row itself.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447

2 Answers2

23

After selecting the row you will have to set the focus on the row in the following way:

ResultsGrid.SelectedIndex = index;
DataGridRow row = (DataGridRow)ResultsGrid.ItemContainerGenerator.ContainerFromIndex(index);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
Guido Zanon
  • 2,939
  • 26
  • 31
1

Try this:

yourDataGrid.SelectedItem = yourDataGrid.Items[i];
Taylor Leese
  • 51,004
  • 28
  • 112
  • 141