user1716420
user1716420

Reputation:

How to disable a row in gridview?

In have to disable a row in which chechbox is checked.I have tried it in RowDataBound event by using following code but it shows error 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;
  }

Upvotes: 0

Views: 16306

Answers (3)

Furqan Safdar
Furqan Safdar

Reputation: 16728

There might be a chance that your CheckBox object is null. So i have also added a null check in the code.

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

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

ADDED based on valuable suggestion from comments, you can even toggle the CheckBox state if the object is null:

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

Upvotes: 1

Sanjay Gupta
Sanjay Gupta

Reputation: 186

Try the following...i assume that checkbox is in gridview rows's Cell....

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

}

Upvotes: 0

Amol Kolekar
Amol Kolekar

Reputation: 2325

Try the following...i assume that checkbox is in gridview rows....

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

}

Upvotes: 2

Related Questions