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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need to set the SortExpression.
<asp:TemplateField HeaderText="Name" SortExpression="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" SortExpression="Address">
<ItemTemplate>
<div align="right"><%# (DataBinder.Eval(Container.DataItem, "HouseNumber"))%></div>
</ItemTemplate>
</asp:TemplateField>
in the codebehind you can store the current SortExpression and the SortDirection in ViewState:
Private Property SortExpression() As String
Get
If ViewState("SortExpression") Is Nothing OrElse ViewState("SortExpression").ToString().Length = 0 Then
ViewState("SortExpression") = "Name ASC"
End If
Return ViewState("SortExpression").ToString
End Get
Set(ByVal value As String)
ViewState("SortExpression") = value
End Set
End Property
and in the Sorting-Handler of the GridView:
Protected Sub theGrid_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles theGrid.Sorting
Dim currentSortColumn, currentSortDirection As String
currentSortColumn = Me.SortExpression.Split(" "c)(0)
currentSortDirection = Me.SortExpression.Split(" "c)(1)
If e.SortExpression.Equals(currentSortColumn) Then
'switch sort direction
Select Case currentSortDirection.ToUpper
Case "ASC"
Me.SortExpression = currentSortColumn & " DESC"
Case "DESC"
Me.SortExpression = currentSortColumn & " ASC"
End Select
Else
Me.SortExpression = e.SortExpression & " ASC"
End If
BindGrid() 'load the data with this SortExpression and DataBind the Grid'
End Sub
Sorry for VB.NET, i’ve noticed too late. You could translate it here.
Edit: C#
private string SortExpression {
get {
if (ViewState("SortExpression") == null || ViewState("SortExpression").ToString().Length == 0) {
ViewState("SortExpression") = "Name ASC";
}
return ViewState("SortExpression").ToString;
}
set { ViewState("SortExpression") = value; }
}
protected void theGrid_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
string currentSortColumn = null;
string currentSortDirection = null;
currentSortColumn = this.SortExpression.Split(' ')[0];
currentSortDirection = this.SortExpression.Split(' ')[1];
if (e.SortExpression.Equals(currentSortColumn)) {
//switch sort direction
switch (currentSortDirection.ToUpper()) {
case "ASC":
this.SortExpression = currentSortColumn + " DESC";
break;
case "DESC":
this.SortExpression = currentSortColumn + " ASC";
break;
}
} else {
this.SortExpression = e.SortExpression + " ASC";
}
//load the data with this SortExpression and DataBind the Grid
BindGrid();
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0