1

I am having trouble with deleting one to multiply rows in DataGridView by selecting check box and clicking the delete button. When i click the delete button nothing happens and neither appears if there is an error.

Here is my code

SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog=DatabaseName;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    SqlDataReader dr;


private void btnDeleteCust_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow item in gridCustomer.Rows)
    {
        if (bool.Parse(item.Cells[6].Value.ToString()))
        {
            cn.Open();
            cmd.CommandText = "delete from Customer where customerID = '" + item.Cells[0].Value.ToString() + "'";
            cmd.ExecuteNonQuery();

            cn.Close();
        }
    }
}
jessmh1
  • 21
  • 6
  • Use debug and see why nothing happens. Also don't use global connection, command and reader. – mybirthname Oct 15 '16 at 08:56
  • put a try catch and see if the app has an error !! i think the problem is with your item.cells[6], try to put nsame instead of index – Amir Ziarati Oct 15 '16 at 08:57
  • When you check the CheckBox, do you validate is it checked or not like `CheckBox.Checked == true`? Get the CheckBox index and try doing so. – AT-2017 Oct 15 '16 at 09:03

1 Answers1

0

What I can see you didn't check whether the CheckBox is checked or not as follows:

if(Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true)

Get the index or position of the CheckBox that are being checked and finally their ids to delete.

For more, see this link - Delete Rows With CheckBox in DataGridView

AT-2017
  • 3,114
  • 3
  • 23
  • 39