I have an exisiting gridview that I need to fix/improve here at work. Basically the GridView has headings and they are being binded by a data set in the code behind, it uses BoundFields and TemplateFields. The problem is that I am needed to make each column sortible. What would be the best approach to do this since it is not the standard gridview?, I need to make the header links and when click to sort in DESC or ASC order. Here is an example of the gridview I need to work on.
<asp:GridView ID="theGrid" runat="server" CssClass="Grid" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#FFFFFF" GridLines="None" OnPageIndexChanging="theGrid_PageIndexChanging" CaptionAlign="Left" OnRowCommand="theGrid_RowCommand" Width="100%" PageSize="25" AllowSorting="True" EmptyDataText="It's Empty.">
<Columns>
<asp:BoundField DataField="TestID" HeaderText="ID"/>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<a href='sometest.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "TestID").ToString()%>'><%# DataBinder.Eval(Container.DataItem, "Type").ToString()%></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<div align="right"><%# (DataBinder.Eval(Container.DataItem, "HouseNumber"))%></div>
</ItemTemplate>
</asp:TemplateField>
…etc, What would be the best way to sort BoundFields and TemplateFields?
You need to set the SortExpression.
in the codebehind you can store the current SortExpression and the SortDirection in ViewState:
and in the Sorting-Handler of the GridView:
Sorry for VB.NET, i’ve noticed too late. You could translate it here.
Edit: C#