views:

29

answers:

2

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?

A: 

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.

Tahbaza
+2  A: 

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";                        
        }
    }
Azhar
This is exactly what I needed. Thank you.
Matt Flowers