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 '不包含'单元格'的定义"
你必须这样检查
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);
}
}
}