0
votes

I have a datagrid and when I load data on this data grid I want to focus on selected row. But I don't get row by index.

if (gridAppointment.SelectedIndex >= 0)
{
   gridAppointment.ScrollIntoView(gridAppointment.Items[gridAppointment.SelectedIndex]);
   DataGridRow row = (DataGridRow)gridAppointment.ItemContainerGenerator.ContainerFromIndex(
         gridAppointment.SelectedIndex);
   if (row != null)
       row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));                                               
 }
1
What about using SelectedItem instead? - Ian of Oz

1 Answers

0
votes

When you select the row, the next condition is true and you are able to get all the cells values like this.

if (gridAppointment.SelectedIndex >= 0)
{
    DataRowView dataRow = (DataRowView)gridAppointment.SelectedItem;
    int i = dataRow.Row.ItemArray[0].ToString();
    // ItemArray[1] is the next column and so on.
}