簡體   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