2

i am using checkbox in gridview..to get the checkbox id i am using the following code..

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        CheckBox chkDelete = (CheckBox)GridView1.Rows.Cells[0].FindControl("chkSelect");
        if (chkDelete != null)
        {
            if (chkDelete.Checked)
            {
                strID = GridView1.Rows.Cells[1].Text;
                idCollection.Add(strID);
            }
        }
    }

BUT THE KEYWORD "CELLS"..do not support..i am getting an error.."System.Web.UI.WebControls.GridViewRowCollection' does not contain a definition for 'Cells' "

1
  • I don't know why you are looping the statement as you are hard coding the values for the cells like cells[0] and cells[1] what if you need other rows data
    – Developer
    Apr 16, 2011 at 9:30

4 Answers 4

3

This is the way you have to check

foreach (GridViewRow grRow in grdACH.Rows)
    {
        CheckBox chkItem = (CheckBox)grRow.FindControl("checkRec");
        if (chkItem.Checked)
        {
            strID = ((Label)grRow.FindControl("lblBankType")).Text.ToString();
         }
}
3
  • if (chkItem.Checked) { }how to decide using above code which checkbox is checked
    – user635545
    Apr 16, 2011 at 9:18
  • You will get the control corresponding to that and if it is checked it will definitely show you as checked have you tried that or else post the design of your aspx page so that i will give as per your requirement
    – Developer
    Apr 16, 2011 at 9:24
  • Name of the gridivew in his case it is default GridView1
    – Developer
    Jul 12, 2011 at 15:40
2

That's correct; the GridViewRowCollection class does not contain either a method or a property with the name Cells. The reason that matters is that the Rows property of the GridView control returns a GridViewRowCollection object, and when you call GridView1.Rows.Cells, it is searching for a Cells property on the GridViewRowCollection object returned by the Row property.

1
for (int i = 0; i < GridView1.Rows.Count; i++)
{
    CheckBox chkDelete = (CheckBox)GridView1.Rows[i].FindControl("chkSelect");
    if (chkDelete != null)
    {

        if (chkDelete.Checked)
        {
            strID = GridView1.Rows[i].Cells[1].Text;
            idCollection.Add(strID);
        }
    }
}
0
 foreach (GridViewRow rowitem in GridView1.Rows)
            {
                CheckBox chkDelete = (CheckBox)rowitem.Cells[0].FindControl("chkSelect");
                if (chkDelete != null)
                {
                    if (chkDelete.Checked)
                    {
                        strID = rowitem.Cells[1].Text;
                        idCollection.Add(strID);
                    }
                }


            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.