Reputation: 153
I want to change the name of one of the column of gridview on runtime.The Gridview has also sorting enabled in it.
the gridview looks like:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" OnRowCreated="myGrid_Rowcreate"
ShowFooter="false"
AllowSorting="true"
OnSorting="GridView1_Sorting"
EnableViewState="false"
ShowHeaderWhenEmpty="True"
AllowPaging="false">
<AlternatingRowStyle CssClass="altrowstyle" />
<HeaderStyle CssClass="headerstyle" />
<RowStyle CssClass="rowstyle" />
<RowStyle Wrap="false" />
<HeaderStyle Wrap="false" />
</asp:GridView>
and my onRowCreate function looks like:
protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;
if (gvr.RowType == DataControlRowType.Header)
{
gvr.Cells[1].Text = "test1";
}
}
the column name does change to test1 but the sorting feature goes off.what should I do that will change the coulmn name as well as the sorting feature also stays?there is no problem in my sorting.Only when I write the above code,the sorting option for that column goes off. Please help! thank you.
Upvotes: 1
Views: 3748
Reputation: 1046
The problem is because Sorting sets a LinkButton control in the GridView columns name, and setting the name in the way you do it, disable that control. So, you must set the name of the column with a code like:
((LinkButton)e.Row.Cells[1].Controls[0]).Text = "Test1";
Upvotes: 2
Reputation: 1115
I think your problem comes from the postback sent when you order your grid. You don't want to rebind your grid after ordering it. In your pageLoad you should add :
If(!PostBack)
// the code to bind data to your grid
This way you prevent the grid to be reloaded and the information you set in myGrid_RowDataBound for the name your column should stay the same...
Upvotes: 1
Reputation: 1366
I'm guessing you're trying to use AutoGenerateColumns?
If this is the case why can't you change the column name in the datasource (using "AS" if the datasource is SQL)?
Otherwise, your altering the cell text is short-circuiting the ASP.NET functionality which generates the sorting javascript postback.
Alternatively you would do this if you don't use AutoGenerateColumns:
myGrid.Columns[1].HeaderText = "test1";
Upvotes: 1