Answered by:
How to sort GridView with multiple columns

Question
-
User-1641476077 posted
I would like to check how to i do sorting for GridView where user is able to click on the table header's column of which he want to sort
my markup
<asp:GridView ID="gvCourse" runat="server" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false" OnSorting="gvCourse_Sorting" CssClass="table table-striped table-bordered table-hover"> <PagerSettings Position="TopAndBottom" Mode="Numeric" /> <Columns> <asp:TemplateField SortExpression="id_ASC" HeaderText="Course ID"> <ItemTemplate> <asp:HyperLink ID="lnkId" runat="server" NavigateUrl='<%# string.Format(manageCourseURL, HttpUtility.UrlEncode(Eval("id").ToString())) %>' Text='<%# Eval("id") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="courseCode" HeaderText="Course Code" ItemStyle-Width = "150" SortExpression="courseCode_ASC" /> <asp:BoundField DataField="courseTitle" HeaderText="Course Title" ItemStyle-Width = "250" SortExpression="courseTitle_ASC" /> <asp:BoundField DataField="courseType" HeaderText="Course Type" ItemStyle-Width = "150" SortExpression="courseType_ASC" /> <asp:BoundField DataField="courseTemplate" HeaderText="Course Template" ItemStyle-Width = "150" SortExpression="courseTemplate_ASC" /> <asp:BoundField DataField="duration" HeaderText="Duration" ItemStyle-Width = "80" SortExpression="duration_ASC" /> <asp:BoundField DataField="status" HeaderText="Status" ItemStyle-Width = "60" SortExpression="status_ASC" /> <asp:BoundField DataField="modifiedDate" HeaderText="Last Modified Date" SortExpression="modifiedDate_ASC" ItemStyle-Width = "200" DataFormatString="{0:d MMMM yyyy hh:mm tt}" htmlencode="false" /> <asp:TemplateField ControlStyle-Width="10"> <HeaderTemplate> <asp:CheckBox ID="chkAll" runat="server" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkRow" runat="server" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code Behind on Sorting
protected void gvCourse_Sorting(object sender, GridViewSortEventArgs e) { DataTable courseTable = ViewState["courses"] as DataTable; if(courseTable != null) { string[] sortExpression = e.SortExpression.Split('_'); string newDirection = ""; if(sortExpression[1] == "ASC") { newDirection = "DESC"; } else if(sortExpression[1] == "DESC") { newDirection = "ASC"; } courseTable.DefaultView.Sort = sortExpression[0] + " " + sortExpression[1]; e.SortExpression = sortExpression[0] + "_" + newDirection; gvCourse.DataSource = courseTable; gvCourse.DataBind(); } }
when i click on the any of the table headers, the sorting is not happening
what i wan is something like that
Monday, July 25, 2016 5:04 AM
Answers
-
User1724605321 posted
Hi ,
bczm8703
the link provided is broken link.Please refer to below links for demo :
http://www.c-sharpcorner.com/uploadfile/nipuntomar/gridview-multicolumn-sorting/
http://dotnetslackers.com/articles/gridview/SortingaGridviewwithmultipleColumns.aspx
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 25, 2016 8:13 AM
All replies
-
User1724605321 posted
Hi bczm8703,
You could refer to below articles for how to sort GridView with multiple columns:
Best Regards,
Nan Yu
Monday, July 25, 2016 6:53 AM -
User-1641476077 posted
Hi Nan Yu,
the link provided is broken link.
Monday, July 25, 2016 7:11 AM -
User1724605321 posted
Hi ,
bczm8703
the link provided is broken link.Please refer to below links for demo :
http://www.c-sharpcorner.com/uploadfile/nipuntomar/gridview-multicolumn-sorting/
http://dotnetslackers.com/articles/gridview/SortingaGridviewwithmultipleColumns.aspx
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 25, 2016 8:13 AM -
User702547207 posted
Since the sorting is done in the data table and then binding to the gridview think the sort order is changing. Instead of sorting the data table try to set the grid view sort expression as shown in the link shared by Nan Yu.
Tuesday, July 26, 2016 6:06 AM