MilvNutt
MilvNutt

Reputation: 69

get row index of selected item in datagrid

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.

Upvotes: 3

Views: 1387

Answers (1)

Isma
Isma

Reputation: 15209

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();
}

Upvotes: 2

Related Questions