1

I created a DataGrid which I linked to a DataBase (Table). My question is how can I delete a selected row (using btn_click) from the DataGrid and also delete the same data from the DataBase (Table).

Thanks in advance!

Pop Pilli
  • 15
  • 1
  • 1
  • 6

1 Answers1

3

You can access the currently selected item of a DataGrid using the SelectedItem property.

Your can then call the Remove method to remove the item from the DataGrid

var selectedItem = myDataGrid.SelectedItem;
if (selectedItem != null)
{
   myDataGrid.Items.Remove(selectedItem);
}

After the first line you need to extract the information (e.g. some Id) from the item in order to delete it in the database. Usually you cast the SelectedItem to the object you used to bind to the grid.

See also this response.

Community
  • 1
  • 1
Martin
  • 5,165
  • 1
  • 37
  • 50