1

I have a gridview and I would like to be able to programatically change the HeaderText of it's columns (probably in the DataBinding event). I know this can normally be achieve with something like this:

myGrid.Columns[0].HeaderText = "My Header Text";

However, the gridview in question is actually nested within another gridview (via template column). So I can't access it directly. I'm trying to use the FindControl method to access it, but so far that isn't working. Any thoughts?

3 Answers 3

3

Capture that child grid in RowDataboud event of parent grid and here you can change the header text Suppose myGrid is Parent Grid and ChildGrid is Child grid..

OnRowDataBound="myGrid_RowDataBound"

protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

           GridView ChildGrid = (GridView)e.Row.FindControl("ChildGrid");
           ChildGrid.Columns[0].HeaderText = "My Header Text";         
            .
            .
           ChildGrid.Columns[n].HeaderText = "My Header Text";                        
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Capture a reference to the nested gv in the itemdatabound event of the topmost gv. You can then try to change your header on the nested gv reference there. If that doesn't do the trick you can always conditionally show/hide placeholders from within the nested gv itemdatabound event when e.item.listitemtype is header.

Comments

-1

Change Header Text Of Gridview Using RowData Bound:

The answer is:

if (e.Row.RowType == DataControlRowType.DataRow){
    Gridview1.Columns[0].HeaderText = "New Header Name";
}

Comments

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.