I have a .NET windows app.
I have a DataGridView which has a few rows in it. I had added a column containing buttons to allow the user to edit a row. This pops up a new form/dialog – my edit form.
// Add a CellClick handler to handle clicks in the button column.
dgv.CellClick += new DataGridViewCellEventHandler(dgv_CellClick);
The code which fires this is:
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == buttonCell)
{
// Get the id number
int number = Convert.ToInt32(dgv.Rows[e.RowIndex].Cells[0].Value);
EditForm edit = new EditForm(number);
edit.ShowDialog();
}
}
Problem
My edit form displays (correctly). I make my changes, and close the form. It then loads again! It does this for the same number of rows in my Grid. Ie. It fires the code above once for each row in the DataGrid.
How can I prevent this? Is there a more elegant solution?
I would make sure you’re not registering the
CellClickevent more than once.i.e., that you’re not calling this code more than once (like each time you add a row to your DGV):