checkBox in gridView

本文关键字:gridView in checkBox | 更新日期: 2023-09-27 17:50:24

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

但是关键字"细胞"..不支持…我得到一个错误…"System.Web.UI.WebControls。gridviewwrowcollection '不包含'单元格'的定义"

checkBox in gridView

你必须这样检查

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

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

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

            }