Alex Rivera | Logout

How to change the Header Text of Gridview after Databound?

Asked 2010-09-21T06:58:22.850
9

I have a gridview I bound a DataTable with that Gridview Its dynamic so no hardcode Text in desin.

I tried to change it after Databound and in PreRender of gridview but no Success.

Actually there are Underscores('_') in text and I want to Replace it with space.

Below is code

<asp:GridView ID="grdSearchResult" runat="server" AutoGenerateColumns="True" Width="99%" OnPreRender="grdSearchResult_PreRender"
            OnRowCreated="grdSearchResult_OnRowCreated" OnPageIndexChanging="grdSearchResult_PageIndexChanging">
            <HeaderStyle ForeColor="White" BackColor="#215B8D" />
            <AlternatingRowStyle BackColor="#F7F7F7" />
            <RowStyle CssClass="gridtext" HorizontalAlign="Center" />
        </asp:GridView>



protected void grdSearchResult_PreRender(object sender, EventArgs e)
{
    for (int i = 0; i < grdSearchResult.Columns.Count; i++)
    {
        grdSearchResult.Columns[i].HeaderText = grdSearchResult.Columns[i].HeaderText.Replace("_", "");
    }
}
Edit
Report

1 Answer

9

You can change the text of the cell rather than the HeaderText property:

        for (int i = 0; i < grdSearchResult.Columns.Count; i++)
        {
            grdSearchResult.HeaderRow.Cells[i].Text = grdSearchResult.HeaderRow.Cells[i].Text.Replace("_", "");
        }

You don't need to do this in PreRender, just after the data has been bound.

answered 2010-09-21T08:26:22.587

Your Answer