33

I have a GridView which I programmatically bind using a a SqlDataAdapter DataSet.

adapter.Fill(ds); 
testGV.DataSource = ds; 
testGV.DataBind();

The problem is that the columns get their header texts directly from the database, which can look odd when presented on websites.

I would like to modify the column header text programmatically. I have already tried the following,

testGV.Columns[0].HeaderText = "Date";

and

this.testGV.Columns[0].HeaderText = "Date";

but they do not seem to give me the correct result.

4
  • 1
    Could you elaborate on "does not help"? Do you get an error? How are you binding data to the grid? Commented Oct 22, 2012 at 14:13
  • why not change the column name using AS keyword while retrieving data from SQL? Commented Oct 22, 2012 at 14:15
  • 1
    change it in the query or the stored procedure you use to get data Commented Oct 22, 2012 at 14:18
  • If you're AllowSorting the gridview, use the Sorted() event to handle the suggestions below, otherwise putting it in RowDataBound() will not work Commented Feb 27, 2016 at 21:28

6 Answers 6

55

You should do that in GridView's RowDataBound event which is triggered for every GridViewRow after it was databound.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Text = "Date";
    }
}

or you can set AutogenerateColumns to false and add the columns declaratively on aspx:

<asp:gridview id="GridView1" 
  onrowdatabound="GridView1_RowDataBound"
  autogeneratecolumns="False"
  emptydatatext="No data available." 
   runat="server">
    <Columns>
         <asp:BoundField DataField="DateField" HeaderText="Date" 
            SortExpression="DateField" />
    </Columns>
</asp:gridview>
Sign up to request clarification or add additional context in comments.

5 Comments

is changing the column name using As keyword while retrieving from SQL a better option? am not sure if its recommended
@TimSchmelter thank you i just wanted to make sure its a good way of doing it
was wondering, how do i call the function protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
GridViewRowEventArgs I get the error this name type of namespace is not knowing!!! how do I resolve it
no actually Im using asp.net in SharePoint environment
19

This should work:

testGV.HeaderRow.Cells[0].Text="Date"

3 Comments

Which GridView event is that best placed in?
It works for me in the DataBound event, but I don't know if that's the best place for it...it's slow...maybe that event gets called a gazillion times...
PreRender seems faster.
4

You can do it with gridview's datarow bound event. try the following sample of code:

protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "TiTle";
}
}

For more details about the row databound event study Thsi....

Comments

2

Better to find cells from gridview instead of static/fix index so it will not generate any problem whenever you will add/remove any columns on gridview.

ASPX:

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" >
    <Columns>
        <asp:BoundField HeaderText="Date" DataField="CreatedDate" />
    </Columns>
</asp:GridView>

CS:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            if (string.Compare(e.Row.Cells[i].Text, "Date", true) == 0)
            {
                e.Row.Cells[i].Text = "Created Date";
            }
        }
    }
}

Comments

1

On your asp.net page add the gridview

<asp:GridView ID="GridView1" onrowdatabound="GridView1_RowDataBound" >
</asp:GridView>

Create a method protected void method in your c# class called GridView1_RowDataBound

as

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Text = "HeaderText";
    }
}

Everything should be working fine.

Comments

0
protected void grdDis_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            #region Dynamically Show gridView header From data base
            getAllheaderName();/*To get all Allowences master headerName*/

            TextBox txt_Days = (TextBox)grdDis.HeaderRow.FindControl("txtDays");
            txt_Days.Text = hidMonthsDays.Value;
            #endregion
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.