Hi,
I have added an unbound check box column in a datagridview. I have used
dataset as the datasource for the DGV. I need to select(check) multiple rows
and the checked rows has to be removed from the grid, also I need to update
the database correspondingly.
I am able to do this for single row using the following code. can you help
me out on selecting multiple rows and updating DB? I am missing some where
and I was not able to get the desired output.
Please find below the code which I used to delete a single row.
private void btn_withdraw_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
try
{
if (row.Cells["Column1"].Value != null)
{
if (bool.Parse(row.Cells["Column1"].Value.ToString()))
{
cmd =
new OleDbCommand();
cmd.CommandType =
CommandType.Text;
cmd.CommandText = withdrawquery;
withdrawquery =
"UPDATE LEAVEMASTER_TABLE SET If_Approved = 'WITHDRAWN' WHERE Emp_ID = '" +
this.dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString()
+ "' AND From_Date = CDate(Format('" +
this.dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString()
+ "', 'mm/dd/yyyy')) AND To_Date = CDate(Format('" +
this.dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString()
+ "' , 'mm/dd/yyyy'))";
cmd =
new OleDbCommand(withdrawquery, conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("LEAVE WITHDRAWN !");
}
foreach (DataGridViewRow row2 in this.dataGridView1.SelectedRows)
{
if (row2.Index != this.dataGridView1.Rows.Count)
{
this.dataGridView1.Rows.Remove(row2);
}
}
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
finally
{
conn.Close();
}
}
}
--
Thanks & Regards,
Karthikeyan