I have a datagridview where the cells on one column are a checkbox item, I would like to remove (on button click) the checked rows. I think I am nearly there with the below but get the following execption thrown:
'Object cannot be cast from DBNull to other types.'
Below is the code inside the button I'm using:
for (int i = 0; i < supplyDataGridView.Rows.Count; i++)
{
if (Convert.ToBoolean(supplyDataGridView.Rows[i]
.Cells[1].Value) == true)
{
supplyDataGridView.Rows.RemoveAt(i);
}
}
Loading
VulpesPosted Feb 12, 2013, 8:37 AM
Upamanyu Roy ChoudhuryPosted Feb 13, 2013, 2:20 AM
There are many approaches to delete the checked entry in a GridView.
However I personally like the following way most
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GrvDraft.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
if ((gvr.FindControl("chkChild") as CheckBox).Checked == true)
{
//Your Delete Code
int i = Convert.ToInt32(GrvDraft.DataKeys[gvr.RowIndex].Value);
}
}
}
}
Another way
GVGLCode1.DataSource = dt;
GVGLCode1.DataBind();
int iCount = GVGLCode1.Rows.Count;
for (int i = 0; i <= iCount; i++)
{
CheckBox cb = (CheckBox)GVGLCode1.rows[i].FindControl("checkBox");
if (cb != null && cb.Checked)
{
GVGLCode1.DeleteRow(i);
}
}
With Warm Regards,
Upamanyu
mike DelvottiPosted Feb 12, 2013, 10:21 AM