繁体   English   中英

如何禁用GridView中的行?

[英]How to disable a row in gridview?

在必须禁用选中chechbox的行中。我已通过使用以下代码在RowDataBound事件中尝试过此操作,但它显示错误Object reference not set to an instance of an object

  CheckBox cbAttachClrReq = (CheckBox)gvEntity.FindControl("chkAdd");

  if (cbAttachClrReq.Checked)
  {
      this.gvEntity.Rows[e.Row.RowIndex].Enabled = false;
  }

您的CheckBox对象可能为null 因此,我还在代码中添加了一个null检查。

if (e.Row.RowType == DataControlRowType.DataRow)
{
    CheckBox cbAttachClrReq = e.Row.FindControl("chkAdd") as CheckBox;

    if (cbAttachClrReq != null && cbAttachClrReq.Checked)
        e.Row.Enabled = false;
}

根据注释中的宝贵建议添加 ,如果对象为null ,您甚至可以切换CheckBox状态:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    CheckBox cbAttachClrReq = e.Row.FindControl("chkAdd") as CheckBox;
    e.Row.Enabled = cbAttachClrReq == null || !cbAttachClrReq.Checked;
}

尝试以下操作...我假设复选框位于gridview行中....

protected void gvEntity_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       CheckBox cbAttachClrReq = (CheckBox) e.Row.FindControl("chkAdd");

      if (cbAttachClrReq.Checked)
      {
          e.Row.Enabled = false;
      }       
    }

}

尝试以下操作...我假设复选框位于gridview行的单元格中。

protected void gvEntity_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       CheckBox cbAttachClrReq = (CheckBox) e.Row.Cells[yourCellIndexOFChBox].FindControl("chkAdd");

      if (cbAttachClrReq.Checked)
      {
          e.Row.Enabled = false;
      }       
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM