Skip to content

To delete checked rows in grid view

Mohamed Shakir

Asked Jul 31, 2014, 1:21 AM EDT1 answer
Share
LinkedInX
C#
private void button3_Click(object sender, EventArgs e)



       {
       //   string query = "DELETE FROM [payments] WHERE date = " + dateToDelete;
           int i = 0;
           List<int> ChkedRow = new List<int>();

           for (i = 0; i <= dataGridView1.RowCount - 1; i++)
           {
               if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column1"].Value) == true)
               {
                   ChkedRow.Add(i);
               }
           }



           if (ChkedRow.Count == 0)
           {
               MessageBox.Show("Select one checkbox");
               return;
           }

           foreach (int j in ChkedRow)
           {
          //     cnnStr = @"DELETE from (date,custname,description,chqno,duedate,accname,amount)
           //                 where ('" + dataGridView1.Rows[j].Cells["dateDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["custnameDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["descriptionDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["chqnoDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["duedateDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["accnameDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["amountDataGridViewTextBoxColumn"].Value + "');";
               cnnStr = "DELETE FROM [payments] WHERE ('" + dataGridView1.Rows[j].Cells["payidDataGridViewTextBoxColumn"].Value + "');";

               try
               {
                   using (SqlConnection cs = new SqlConnection(ConnectionString))
                   {

                       using (cmd = new SqlCommand(cnnStr, cs))
                       {
                           cs.Open();
                           cmd.ExecuteNonQuery();
                       }
                   }
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               }
           }
           MessageBox.Show("Records deleted Succesfully");





        }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    what's the issue you're facing?
    0