How to change the Header Text of Gridview after Databound?

45,102

Solution 1

Can do this with RowDataBound event of GridView

protected void grdSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.Header)
     {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace("_", " ");
        }
     }
}

and it works fine.

Solution 2

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.

Solution 3

Set AutoGenerateColumns property of gridview to false and add BoundFields.

<asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="false">
<columns>
<asp:BoundField HeaderText="ID" DataField="empNo" />
<asp:BoundField HeaderText="First Name" DataField="fName" />
<asp:BoundField HeaderText="Last Name" DataField="lName" />
</columns>
</asp:GridView>
Share:
45,102
Azhar
Author by

Azhar

.net / iOS / Android / Windows Mobile/ React-native Optimization Optimization Optimization

Updated on August 25, 2020

Comments

  • Azhar
    Azhar over 3 years

    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("_", "");
        }
    }
    
  • Brissles
    Brissles over 13 years
    That will work, it'll just be done every time a row is bound.
  • emragins
    emragins almost 13 years
    Got this to work using grdSearchResult.HeaderRow.Cells.Count instead of grdSearchResult.Columns.Count
  • kenrogers
    kenrogers almost 11 years
    This should be a comment, not an answer.
  • Zarepheth
    Zarepheth almost 11 years
    Yeah, I'm running into this problem... The HeaderRow property is null when I attempt to use it during the Page_Load event. Searching here to see when, where, and how I ought to access the header.
  • Jahaziel
    Jahaziel over 6 years
    This one served my purpose. Called right after gridview.DataBind() in page_load event.