I have a gridview and I would like to be able to programati开发者_高级运维cally 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?
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";
}
}
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.
Change Header Text Of Gridview Using RowData Bound:
The answer is:
if (e.Row.RowType == DataControlRowType.DataRow){
Gridview1.Columns[0].HeaderText = "New Header Name";
}
精彩评论