Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Here in Gridview by using checkbox and one button i wanted to delete the row .I written this code but it's showing error.

ERROR:Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index
C#
protected void btnDelete_Click(object sender, EventArgs e)
    {
        //Loop through all the rows in gridview
        foreach (GridViewRow GridViewrow in GridView1.Rows)
        {
            //Finiding checkbox control in gridview for particular row
            CheckBox chkdelete = (CheckBox)GridViewrow.FindControl("chkdelete");
            //Condition to check checkbox selected or not
            if (chkdelete.Checked)
            {
                //Getting UserId of particular row using datakey value
             int Sno = Convert.ToInt32(GridView1.DataKeys[GridViewrow.RowIndex].Value.ToString());
                SqlDataSource1.DeleteParameters["Sno"].DefaultValue =Sno.ToString();
                SqlDataSource1.Delete();
            }
        }
Posted
Updated 4-Mar-15 23:00pm
v2
Comments
Which line?
Member 11500419 5-Mar-15 7:57am    
int Sno = Convert.ToInt32(GridView1.DataKeys[GridViewrow.RowIndex].Value.ToString());
Debug and see what is the RowIndex here.
Member 11500419 6-Mar-15 0:01am    
ERROR:Index was out of range. Must be non-negative and less than the size of the collection.

online:int Sno = Convert.ToInt32(GridView1.DataKeys[GridViewrow.RowIndex].Value.ToString());

In this line exactplace of that error is[GridViewrow.RowIndex]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900