My datagridview itemDelete function:
this.dgv_items.RowsRemoved += this.dgv_items_itemDelete;
private void dgv_items_itemDelete(object sender, DataGridViewRowsRemovedEventArgs e)
{
try
{
int row = e.RowIndex;
string name = dgv_items.Rows[row].Cells[0].Value.ToString();
deleteFromDB(name);
}
catch (Exception) { }
}
But by the time we reach this code, the row will have been removed, meaning dgv_items.Rows[row].Cells[0].Value
gets the value if the row next in line.
I want to get the Cells[0]
value of the removed row, so I can remove the item from the database file as well. How can I achieve this?
You could handle the UserDeletingRow
event instead. Note that it supports event cancelation.
You could temporarily save the value of the cell in a variable when you select the row using the RowEnter event:
private void dgv_items_RowEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
// tempValue is a class var
tempValue = dgv_items.Rows[e.RowIndex].Cells[0].Value.ToString();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
Then retrieve the tempValue before deleting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With