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

VulpesRank #55 · 29.2K reputationPosted Feb 12, 2013, 3:37 AM EST
mike DelvottiRank #7,298 · 244 reputationPosted Feb 12, 2013, 5:21 AM EST
Upamanyu Roy Choudhury146 reputationPosted Feb 12, 2013, 9:20 PM EST
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