3

I am trying to get the row index for the selected item in a data grid bound to a DataTable.

Here is my attempt (based on this SO answer):

private void ShowRowIndex_Btn(object sender, RoutedEventArgs e)
{
    int editedRowIndex = myDataGrid.Items.IndexOf(myDataGrid.CurrentItem);
    MessageBox.Show(editedRowIndex.ToString());
}

<DataGrid CellEditEnding="PriceListDG_CellEditEnding"  RowEditEnding="MyDataGrid_RowEditEnding" Name="priceListDataGrid" />

Unfortunately I always get -1 as the result.

Isma
  • 14,604
  • 5
  • 37
  • 51
MilvNutt
  • 69
  • 6

1 Answers1

2

If you want to get the index of the row currently being edited, you can do it directly inside your RowEditEnding event:

private void OnRowEditEnding(object sender, .DataGridRowEditEndingEventArgs e)
{
    var index = e.Row.GetIndex();
}
Isma
  • 14,604
  • 5
  • 37
  • 51