Click here to Skip to main content
16,017,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a datagridview at run time. The gridview is populated with data from access database. I have also created a Button Column in the gridview that contains a delete button for each row in the gridview. The event handler is the "DataGridView.CellContentClick"

Now when I click the delete button, message box asks for confirmation. When confirmed for yes question is deleted else it must do nothing.
The code works fine but the problem comes when the code has run for the first time.
Second time it automatically calls the the event handler "DataGridView.CellContentClick" without pressing the delete button.

I can not sort out the problem. Please Help.
Here is the code:

C#
void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   int count = 0;
   var senderGrid = (DataGridView)sender;
   if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
   {
      if (e.ColumnIndex == dataGridView.Columns["DeleteButton"].Index)
      {
         if (dataGridView.Rows[e.RowIndex].IsNewRow != true)
         {
            int qno;
            qno = e.RowIndex + 1;

            DialogResult choice = MessageBox.Show("Are you sure want to delete this    Question?\nDeleted question can not be reovered.", "Delet Question", MessageBoxButtons.YesNo);

                 
            //Delete the question from the table.
            if (choice == DialogResult.Yes)
            {
               dataGridView.Rows.RemoveAt(qno - 1);
               queryDelete = "delete from " + subject + " where QueNo=" + qno + "and SetNo=" + setNo;
               try
               {
                  connection.Open();
                  command = new OleDbCommand(queryDelete, connection);
                  command.ExecuteNonQuery();                        
               }
               catch (Exception ex)                            
               {
                  MessageBox.Show(ex.ToString());
               }                         
               connection.Close();
               MessageBox.Show("Question Deleted.", "Deleted");
            }
            else
            {
               /* Do Nothing */
            }
         }
      }
   }
}
Posted
Updated 27-Aug-14 3:56am
v2

1 solution

Look at the documentation for DialogResult[^].

The DialogResult object is a windows form, which means that it needs to be disposed. The best way to implement this is:

C#
using(DialogResult choice = MessageBox.Show("Are you sure want to delete this Question?\nDeleted question can not be reovered.", "Delet Question", MessageBoxButtons.YesNo))
{
    //Delete the question from the table.
    if (choice == DialogResult.Yes)
    {
        dataGridView.Rows.RemoveAt(qno - 1);
        queryDelete = "delete from " + subject + " where QueNo=" 
        + qno + "and SetNo=" + setNo;
        try
        {
            connection.Open();
            command = new OleDbCommand(queryDelete, connection);
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        connection.Close();
        MessageBox.Show("Question Deleted.", "Deleted");
    }
    else
    {
        /* Do Nothing */
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900