0

hi

i want to delete multiple report in grid view and i am facing some problem please help me as i am new to Asp.net.

 protected void LinkButton1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("chkSelect");
            if (cb.Checked)
            {
                int rid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                SqlDataSource1.DeleteParameters["id"].DefaultValue = rid.ToString();
                SqlDataSource1.Delete();
            }
            }
    }

Edited by Nick Evan: Fixed formatting

2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by rohand
0

Just make sure that "DataKeyNames" property is set with any filed id(i.e EmployeeID, CustomerID etc). When you have set DataKeyNames property, Gridview control automatically create DataKey object for each row.

Here, It may be possible that you haven't set DataKeyNames and you are trying to retrieve the DataKey for a particualr row.

int rid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

So if not set then Set it with Gridview tag in design.

Let us know if it will not solve your problem.

hi

i want to delete multiple report in grid view and i am facing some problem please help me as i am new to Asp.net.

protected void LinkButton1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkSelect");
if (cb.Checked)
{
int rid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
SqlDataSource1.DeleteParameters["id"].DefaultValue = rid.ToString();
SqlDataSource1.Delete();
}
}
}

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.