to delete checked rows in grid view

Jul 31 2014 5:21 AM
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");
}
 
 
 
 
 

Answers (1)