33

I have a GridView which i programmatically bind using c# code. The problem is, the columns get their header texts directly from Database, which can look odd when presented on websites. So basically, i would like to modify the column header text, but programmatically. i have already tried the following,

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

and

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

does not seem to give me correct result.

Mana
  • 1,925
  • 6
  • 39
  • 55
  • Could you elaborate on "does not help"? Do you get an error? How are you binding data to the grid? – Malice Oct 22 '12 at 14:13
  • why not change the column name using AS keyword while retrieving data from SQL? – Karthik Oct 22 '12 at 14:15
  • 1
    basically im using, SqlDataAdapter DataSet --------- adapter.Fill(ds); testGV.DataSource = ds; testGV.DataBind(); --------- – Mana Oct 22 '12 at 14:16
  • 1
    change it in the query or the stored procedure you use to get data – Karthik Oct 22 '12 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 – Fandango68 Feb 27 '16 at 21:28

6 Answers6

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>
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
19

I Think this Works:

 testGV.HeaderRow.Cells[0].Text="Date"
Tristan
  • 3,301
  • 8
  • 22
  • 27
basim
  • 231
  • 2
  • 3
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....

Ram Singh
  • 6,664
  • 35
  • 100
  • 166
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";
            }
        }
    }
}
Jitendra G2
  • 1,196
  • 7
  • 14
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.

Amir Md Amiruzzaman
  • 1,911
  • 25
  • 24
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
        }
    }
Code
  • 679
  • 5
  • 9