Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a gridview, whose header takes the value from code behind.
C#
if (e.Row.RowType == DataControlRowType.Header)
        {
          e.Row.Cells[2].Text = System.DateTime.Now.AddMonths(-1).ToString("MMMM") ;
        }

I would like to enable the sorting function for the last column.
Below are the .aspx code for the gridview.
ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="GridView1_Sort" AllowPaging="True" PageSize="25" GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated">
     <-Columns>
<asp:TemplateField HeaderText="City" SortExpression="City" ItemStyle-HorizontalAlign="Left" HeaderStyle-VerticalAlign="Bottom" ItemStyle-Wrap="False">
<ItemTemplate>
<asp:Label ID="lblCITY" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"City") %>' </></asp:TemplateField>
<asp:TemplateField HeaderText="State" SortExpression="State" ItemStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Bottom">
<ItemTemplate>
<asp:Label ID="lblSTATE" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"State") %>' />
</asp:TemplateField>
<asp:TemplateField SortExpression=???? ItemStyle-HorizontalAlign="Left" HeaderStyle-VerticalAlign="Bottom" ItemStyle-Wrap="false">
</ItemTemplate>
<asp:Label ID="lblMonth_3"   runat="server">
</asp:TemplateField>
</column>
</asp:gridview>

The last column has to be sortable. the first two column works fine. Help me solve this.

Thanks.
Posted
Updated 15-Oct-12 8:39am
v3
Comments
Kenneth Haugland 15-Oct-12 14:41pm    
You should check my edit, not sure I have gotten it exactly right...
sedwa 15-Oct-12 14:45pm    
oh yeah.. it is right..!!
Herman<T>.Instance 14-Nov-12 10:55am    
you could also define the onsorting event in code behind. In that case you can set the sortExpression.
grdView.Sorting += ....
ie: grdFonotheek.Sorting +=new GridViewSortEventHandler(grdFonotheek_Sorting);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900