Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的分类不起作用?_C#_Asp.net_Sorting - Fatal编程技术网

C# 为什么我的分类不起作用?

C# 为什么我的分类不起作用?,c#,asp.net,sorting,C#,Asp.net,Sorting,我正在asp.NETGridView上使用排序,并且已经设置好了所有内容,但它不起作用。这些列甚至不像普通列那样加下划线,单击时什么也不会发生 网格: <asp:GridView ID="gvResults" runat="server" Width="100%" AllowSorting="True" OnSorting="gvResults_Sorting" AutoGenerateColumns="False" CssClass="tblBrowse"> 转换排序方向:

我正在asp.NETGridView上使用排序,并且已经设置好了所有内容,但它不起作用。这些列甚至不像普通列那样加下划线,单击时什么也不会发生

网格:

 <asp:GridView ID="gvResults" runat="server" Width="100%" AllowSorting="True" OnSorting="gvResults_Sorting" AutoGenerateColumns="False" CssClass="tblBrowse">
转换排序方向:

 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;
        }
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";
    }
}
设置每列的属性。例如,
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 ConvertSortDirection(SortDirection sortDirection)
        {
            string newSortDirection = string.Empty;

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

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

            return newSortDirection;
        }
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\u排序中

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

是否为每列设置了
SortExpression
属性?是否检查页面加载是否正常?这意味着您仅在非回发情况下绑定…@ekad我应该在排序表达式中输入什么?@connersz请参见下面的答案。好的,我添加了它,它在某种程度上起到了作用,但它只会在一个方向上排序一次,然后当您再次单击它时,它不会执行任何操作。