Scott G

Scott G

  • NA
  • 103
  • 7.1k

Delete from database using datagrid

Dec 14 2018 9:27 AM
I have a datagrid that is displaying the contents of a SQL table. I am trying to add a button that will delete the selected row from the datagrid. I found the following that allows me to remove it from the grid but it is not removing it from the table of course. I am drawing a blank on how to remove from the grid and the underlying table.
 
foreach (DataGridViewRow row in myGrid.SelectedRows)
if (!row.IsNewRow) myGrid.Rows.Remove(row);
int selectedCount = myGrid.SelectedRows.Count;
while (selectedCount > 0)
{
if (!myGrid.SelectedRows[0].IsNewRow)
myGrid.Rows.RemoveAt(myGrid.SelectedRows[0].Index);
selectedCount--;
}

Answers (2)