Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change header text of columns in a GridView

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.

like image 638
Mana Avatar asked Oct 22 '12 14:10

Mana


2 Answers

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> 
like image 164
Tim Schmelter Avatar answered Sep 28 '22 04:09

Tim Schmelter


I Think this Works:

 testGV.HeaderRow.Cells[0].Text="Date" 
like image 37
basim Avatar answered Sep 28 '22 02:09

basim