Why is my sorting not working?

I use sorting in gridview asp.net and everything is customizable, but this does not work. The columns are not even underlined, as usual, and when clicked, nothing happens.

Grid:

 <asp:GridView ID="gvResults" runat="server" Width="100%" AllowSorting="True" OnSorting="gvResults_Sorting" AutoGenerateColumns="False" CssClass="tblBrowse">

Sorting:

 protected void gvResults_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DataTable dtSortTable = gvResults.DataSource as DataTable;

                if (dtSortTable != null)
                {
                    DataView dvSortedView = new DataView(dtSortTable);

                    dvSortedView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);

                    gvResults.DataSource = dvSortedView;
                    gvResults.DataBind();

                }
            }
            catch (Exception ex)
            {
                ExceptionHandling.NETException(ex, constPageID, constIsSiteSpecific);
            }
        }

Convert Sort Direction:

 private string ConvertSortDirection(SortDirection sortDirection)
        {
            string newSortDirection = string.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }
+4
source share
1 answer

Set the SortExpression property for each column. For example, it SortExpression="Column1"means sort by Column1, SortExpression="Column2"means sort by Column2, etc. Here's what the aspx code looks like:

<asp:GridView ID="gvResults" runat="server" Width="100%" AllowSorting="True" OnSorting="gvResults_Sorting" AutoGenerateColumns="False" CssClass="tblBrowse">
    <Columns>
        <asp:BoundField DataField="Column1" SortExpression="Column1" />
        <asp:BoundField DataField="Column2" SortExpression="Column2" />
    </Columns>
</asp:GridView>

We need to save the last sort expression and the sort direction between postbacks, so these two properties are needed:

private string SortExpression
{
    get
    {
        return ViewState["SortExpression"] == null ? string.Empty : ViewState["SortExpression"].ToString();
    }
    set
    {
        ViewState["SortExpression"] = value;
    }
}

private string SortDirection
{
    get
    {
        return ViewState["SortDirection"] == null ? string.Empty : ViewState["SortDirection"].ToString();
    }
    set
    {
        ViewState["SortDirection"] = value;
    }
}

This is the method to get the following sort direction:

private string GetSortDirection(string sortExpression)
{
    if (sortExpression == this.SortExpression)
    {
        // reverse the sort direction when current sort expression is the same as the last time
        if (this.SortDirection == "ASC")
        {
            return "DESC";
        }
        else
        {
            return "ASC";
        }
    }
    else
    {
        // always return ASC when current sort expression is different than the last time
        return "ASC";
    }
}

, gvResults_Sorting:

protected void gvResults_Sorting(object sender, GridViewSortEventArgs e)
{
    try
    {
        DataTable dtSortTable = gvResults.DataSource as DataTable;

        if (dtSortTable != null)
        {
            // get sort direction (ASC or DESC)
            string sortDirection = GetSortDirection(e.SortExpression);

            DataView dvSortedView = new DataView(dtSortTable);

            dvSortedView.Sort = e.SortExpression + " " + sortDirection;

            gvResults.DataSource = dvSortedView;
            gvResults.DataBind();

            // save current sort expression and sort direction to ViewState
            this.SortExpression = e.SortExpression;
            this.SortDirection = sortDirection;
        }
    }
    catch (Exception ex)
    {
        ExceptionHandling.NETException(ex, constPageID, constIsSiteSpecific);
    }
}
+3

All Articles