Why does my sorting not work?

connersz

I am using sorting on an asp.net gridview and have set everything up but it does not work. The columns aren't even underlined like normal and when clicking nothing happens.

The 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;
        }
ekad

Set the SortExpression property for each column. For example SortExpression="Column1" means sort by Column1, SortExpression="Column2" means sort by Column2, and so on. Here's how the aspx code should look 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 keep the last sort expression and 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 next 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";
    }
}

And finally in 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);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related