Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6000907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:45:39+00:00 2026-05-23T00:45:39+00:00

I have a gridview with 2 columns. I want to learn coding behind and

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:45:40+00:00Added an answer on May 23, 2026 at 12:45 am

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview with couple of columns,I want to achieve the following: If
I have a GridView with 10 columns. On a certain condition, I want to
I have a ListView with a GridView with 3 columns. I want last column
I have a gridview to display data, and one of columns was the file
I have a GridView on my page. I want to set a cell click
I have a gridview and I want to add rows in it dynamically. I
I have a gridview, with some columns. I want to hide one column, but
I have a GridView with three databound columns, like this: <Columns> <asp:BoundField HeaderText=Type DataField=Type
I have a gridview controller with a set of BoundField columns to my data
In gridview's column i have a linkbutton and a label under it. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.