We help IT Professionals succeed at work.

Problem deleting row in a dataGridView

Hi,

I hope this problem turns out to be simple, because it does look like a simple one.

Basically I delete rows from an unbound dataGridView using a columnButton.
Every thing runs smoothly until I try to add a messageBox that asks the user to confirm that he actually wants to delete the row...

When the OK button is selected nothing happens. No rows get deleted.

If I remove the code for the MessageBx things work fine again.

Please check my code below. Thanks.

if (rowToUpdate > -1 && collToUpdate == 1)
                {
                    MessageBox.Show("You are about to delete the record", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        dataGridView1.Rows.RemoveAt(e.RowIndex);
                    }
                    
                }

Open in new window

Comment
Watch Question

Research & Development Manager
Commented:
Try the following:
if (rowToUpdate > -1 && collToUpdate == 1)
                {
                    DialogResult result = MessageBox.Show("You are about to delete the record", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        dataGridView1.Rows.RemoveAt(e.RowIndex);
                    }
                    
                }

Open in new window

Author

Commented:
Thank you...