gridView中的checkBox

时间:2011-04-16 06:19:13

标签: c# asp.net gridview webforms

我正在使用gridview中的复选框来获取复选框ID我正在使用以下代码..

    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);
            }
        }
    }

但关键词“CELLS”..不支持..我收到错误..“System.Web.UI.WebControls.GridViewRowCollection'不包含'Cells'的定义”

4 个答案:

答案 0 :(得分:3)

这是您必须检查的方式

foreach (GridViewRow grRow in grdACH.Rows)
    {
        CheckBox chkItem = (CheckBox)grRow.FindControl("checkRec");
        if (chkItem.Checked)
        {
            strID = ((Label)grRow.FindControl("lblBankType")).Text.ToString();
         }
}

答案 1 :(得分:2)

这是正确的; GridViewRowCollection class不包含名称为Cells的方法或属性。重要的是Rows控件的GridView属性返回GridViewRowCollection对象,当您调用GridView1.Rows.Cells时,它正在搜索Cells GridViewRowCollection属性返回的Row对象上的属性。

答案 2 :(得分: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);
        }
    }
}

答案 3 :(得分: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);
                    }
                }


            }