Get cell value of deleted row DataGridView

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 line will be deleted, and the value dgv_items.Rows[row].Cells[0].Value will get the value if the line is next to the line.

I want to get the value Cells[0] deleted row, so I can also delete the item from the database file. How can I achieve this?

+4
source share
2 answers

Instead, you can handle UserDeletingRow . Please note that it supports event cancellation.

+6
source

You can temporarily store the cell value in a variable when you select a 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 extract tempValue before uninstalling.

+1
source

All Articles