Getting the selected index of a table in a datagrid
Hi everyone. next question. I constructed a table, and added it to a datagrid.
Pseudocode:
// Create table.
// Add values
DataGrid.DataSource = DataTable;
It is working fine. I'm getting information to add. However, when I move to delete I want to get the selected index that the user selected. Does anyone know how to do this?
Where do I get the selectedindex from, the datagrid or the table?
Jennifer
Re: Getting the selected index of a table in a datagrid
The SelectedRowIndex property of the DataGrid indicates the index of the current row in the table's DefaultView, which doesn't necessarily correspond to the index of the same row in the DataTable itself. You could call the Delete method of the DataRowView or the actual DataRow it corresponds to:
Code:
myTable.DefaultView[myGrid.CurrentRowIndex].Delete();
or:
Code:
myTable.DefaultView[myGrid.CurrentRowIndex].Row.Delete();
I believe that you can also use the Postition property of a BindingManagerBase object associated with the DataGrid, but I've never done that myself so I can't offer any specific advice except to perhaps start with the DataGrid.BindingContext property if you're interested.