locked
How to sort GridView with multiple columns RRS feed

  • 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

All replies