为什么我的排序不起作用?

时间:2014-01-20 15:42:13

标签: c# asp.net sorting

我在asp.net gridview上使用排序并设置了一切,但它不起作用。这些列甚至没有像普通的那样加下划线,当点击没有任何反应时。

网格:

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

排序:

 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);
            }
        }

转换排序方向:

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

1 个答案:

答案 0 :(得分:3)

为每列设置SortExpression属性。例如,SortExpression="Column1"表示按Column1排序,SortExpression="Column2"表示按Column2排序,依此类推。以下是aspx代码的外观:

<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>

我们需要在回发之间保留最后一个排序表达式和排序方向,因此需要这两个属性:

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

这是获取下一个排序方向的方法:

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);
    }
}