Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get cell value of removed DataGridView row

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?

like image 775
natli Avatar asked Jul 16 '12 22:07

natli


2 Answers

You could handle the UserDeletingRow event instead. Note that it supports event cancelation.

like image 172
Luis Quijada Avatar answered Sep 30 '22 18:09

Luis Quijada


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.

like image 25
Nic Avatar answered Sep 30 '22 18:09

Nic