user2985344
user2985344

Reputation: 91

dataGridView1_RowsRemoved not working as I expect

I have the following code that load my windows form:

    private void Panou_Load(object sender, EventArgs e)
    {
        List<string>[] list;

        list = Conexiune.Select();
        dataGridView1.Rows.Clear();

        (dataGridView1.Columns[3] as DataGridViewComboBoxColumn).DataSource = new List<string> { "", "activ", "inactiv", "neverificat", "blocat" };

        for (int i = 0; i < list[0].Count; i++)
        {
            int number = dataGridView1.Rows.Add();
            dataGridView1.Rows[number].Cells[0].Value = list[0][i];
            dataGridView1.Rows[number].Cells[1].Value = list[1][i];
            dataGridView1.Rows[number].Cells[2].Value = list[2][i];
            dataGridView1.Rows[number].Cells[3].Value = list[3][i];
            dataGridView1.Rows[number].Cells[4].Value = list[4][i];
            dataGridView1.Rows[number].Cells[5].Value = list[5][i];
            dataGridView1.Rows[number].Cells[6].Value = list[6][i];
        }
    }

Everything works normally but now I want to make some updates in database when I remove a row so I use: dataGridView1_RowsRemoved .

Example:

private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
    MessageBox.Show("deleted");
}
  1. Why the message "deleted" is showing up when the form load?

  2. dataGridView1.Rows[number].Cells[0].Value contain the id from database. How I retrieve this id in the dataGridView1_RowsRemoved function?

Upvotes: 1

Views: 932

Answers (1)

David Hall
David Hall

Reputation: 33153

The actual binding of the data source (your list) to the DataGridView happens after the form load event, and so a lot of events that your would not expect to be fired are fired in the process of data binding.

I don't know why that is - would need to dig through the code or the DataGridView component itself, but there is fortunately a work around - attach all such events during the event handler of the DataBindingComplete event.

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    dataGridView1.RowsRemoved += new DataGridViewRowsRemovedEventHandler(dataGridView1_RowsRemoved);        
}

That way you will not see RowsRemoved fired at form load.


Even better in your case, the answer to the second part of your question involves using a different event, that behaves as expected, even when not attached during DataBindingComplete.

The problem with RowsRemoved is that it fires after the row has been removed, so even though you have the (old) index of the row from the event args, getting to the data is very tricky (maybe impossible?)

The fix is to instead handle the UserDeletingRow event:

void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
    var msg = e.Row.Cells[0].Value.ToString();

    MessageBox.Show(msg);
} 

This event is fired once for every row that is deleted - it would probably make sense to aggregate these events and then perform a single database action from within the RowsRemoved handler.

Upvotes: 3

Related Questions