0

GridView

In this GridView, I want to disable all the CheckBox in the GridView during the View Mode. I can disable the GridView Row cells like

foreach (GridViewRow GVR in gvPODetails.Rows)
    {
        GVR.Cells[1].Enabled =          
        GVR.Cells[11].Enabled = false;
    }

But I dont know how to disable the checkbox in the HeaderTemplate of the Template Field. How to do this?

2
  • You may set checkbox Enabled property to false or explain what do you want to achieve. Commented Dec 3, 2011 at 6:38
  • @Yuriy: I edited my question please look at it.. Commented Dec 3, 2011 at 7:14

2 Answers 2

1

Recipe:

  1. Handle OnRowDataBound on your GridView
  2. Detect whether you are iterating through the header by using e.Row.RowType == DataControlRowType.Header and get a reference to your checkbox control using e.Row.FindControl(checkBoxID)
  3. Set the Enabled property to False
Sign up to request clarification or add additional context in comments.

Comments

1

Try this code:

void GridView_RowDataBound(Object sender, GridViewRowEventArgs e) 
{
    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header ) {
        MyClass myObj = (myObj)e.Row.DataItem;
        CheckBox cb = (CheckBox)e.Row.FindControl("myCheckBox");
        cb.Enabled=false;        
    }
}

And if u want to do using javascript then click here
http://forums.asp.net/t/1742352.aspx/1?How+to+enable+and+disable+CheckBox+in+the+GridView+using+JavaScript+

1 Comment

Your code disable all checkboxes as you've written if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header ) but the asker wanted to disable the checkbox in the header so it should be as : if (e.Row.RowType == DataControlRowType.Header ) and then disable here. Thanks for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.