Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: When and how to dynamically change Gridview's headerText's in code behind?

I have a gridview with 2 columns. I want to learn coding behind and do NOT want to do this in the aspx file. How do I set the header text for my columns dynamically? At what point do I do so? After the adapter has filled the gridview with data? Right now, I have the header text but it is exactly the same as the datafield name which is last_name and I want to see Last Name in the header field instead. I've tried

GridView1.Columns[0].HeaderText = "Last Name";

but wherever I tried to put it, the compiler complains about index out of range.

Thanks.

aspx code for the gridview:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
                Width="728px" CellPadding="4" ForeColor="Black" GridLines="Vertical" OnPageIndexChanging="GridView1_PageIndexChanging"
                OnSorting="GridView1_Sorting" PageSize="14" OnRowDataBound="GridView1_RowDataBound">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#CCCC99" />
                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                <RowStyle BackColor="#F7F7DE" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FBFBF2" />
                <SortedAscendingHeaderStyle BackColor="#848384" />
                <SortedDescendingCellStyle BackColor="#EAEAD3" />
                <SortedDescendingHeaderStyle BackColor="#575357" />
                <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last"
                    PageButtonCount="5" Position="Bottom" />
            </asp:GridView>
like image 885
user776676 Avatar asked Dec 03 '22 08:12

user776676


1 Answers

Try putting it in the GridView1.RowDataBound handler. Evaluate e.Row.RowType to determine if it is a header row, then then replace the HeaderText.

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header) {
        GridView1.Columns[0].HeaderText = "Last Name";

    }
}

If you are dynamically creating the columns and using sorting, however, you will need to approach it this way to prevent the incidental conversion of the link to sort into plain text:

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header) {
        LinkButton HLink = (LinkButton)e.Row.Cells[0].Controls[0];
        HLink.Text = "Last Name";
    }
}

With either, add this attribute to your Gridview in the ASPX:

OnRowDataBound="GridView1_RowDataBound"
like image 97
Joe Avatar answered Dec 21 '22 22:12

Joe