Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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# 如何按列降序排列GridView?_C#_Asp.net_Gridview - Fatal编程技术网

C# 如何按列降序排列GridView?

C# 如何按列降序排列GridView?,c#,asp.net,gridview,C#,Asp.net,Gridview,如果我有下面这样的网格视图。如何以最简单的方式按ID(始终)描述顺序排序?我是否需要一个分拣表达式?我对这真的很陌生,所以要求学习 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="gridView_Sorting"> <Columns> <asp:Hype

如果我有下面这样的网格视图。如何以最简单的方式按ID(始终)描述顺序排序?我是否需要一个
分拣表达式
?我对这真的很陌生,所以要求学习

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="gridView_Sorting">
            <Columns>
                <asp:HyperLinkField DataTextField="ID" DataNavigateUrlFields="ID"
                    DataNavigateUrlFormatString="CrimeCoordinator.aspx?ID={0}" Text="Lead ID"
                    HeaderText="Ärendenummer" />
                <asp:BoundField DataField="Employee" HeaderText="Handläggare" />
            </Columns>
</asp:GridView>

由于您使用对象列表作为数据源,因此您需要实现自己的排序逻辑,如下所示:

public sealed class GenericComparer<T> : IComparer<T>
{
    public enum SortOrder
    {
        Ascending = 0,
        Descending = 1
    }

    private string sortColumn;
    private SortOrder sortingOrder;

    public string SortColumn
    {
        get
        {
            return this.sortColumn;
        }
    }

    public SortOrder SortingOrder
    {
        get
        {
            return this.sortingOrder;
        }
    }

    public GenericComparer(string theSortColumn, SortOrder theSortingOrder)
    {
        this.sortColumn = theSortColumn;
        this.sortingOrder = theSortingOrder;
    }

    public int Compare(T x, T y)
    {
        PropertyInfo thePropertyInfo = typeof(T).GetProperty(this.sortColumn);
        IComparable object1 = (IComparable)thePropertyInfo.GetValue(x, null);
        IComparable object2 = (IComparable)thePropertyInfo.GetValue(y, null);
        if (this.sortingOrder == SortOrder.Ascending)
        {
            return object1.CompareTo(object2);
        }
        else
        {
            return object2.CompareTo(object1);
        }
    }
}
公共密封类GenericComparer:IComparer
{
公共枚举排序器
{
升序=0,
递减=1
}
私有字符串排序列;
私人分拣员分拣订单;
公共字符串排序列
{
得到
{
返回此.sort列;
}
}
公共分拣机分拣订单
{
得到
{
返回此.sortingOrder;
}
}
公共泛型比较器(字符串排序列,排序顺序排序器排序顺序)
{
this.sort列=排序列;
this.sortingOrder=排序顺序;
}
公共整数比较(TX,TY)
{
PropertyInfo thePropertyInfo=typeof(T).GetProperty(this.sortColumn);
IComparable object1=(IComparable)thePropertyInfo.GetValue(x,null);
IComparable object2=(IComparable)thePropertyInfo.GetValue(y,null);
if(this.sortingOrder==SortOrder.Ascending)
{
返回object1.CompareTo(object2);
}
其他的
{
返回object2.CompareTo(object1);
}
}
}
现在,在对对象列表上的
.Sort()
方法的调用中,您传递了该帮助器类的一个新实例(将要排序的列表属性和排序方向传递给它-升序或降序)


由于上面的比较器逻辑使用泛型,因此可以传递任何要排序的类型(即
int
DateTime
,甚至是整个域对象)。

通过对象列表,可以根据对象的属性对列表进行排序。 在将列表作为数据源分配给gridview之前,应该对codebehind中的列表进行排序

下面是一个如何按ID对员工列表进行排序的示例。排序由linq执行,因此请记住在代码隐藏中添加linq作为引用

using System.Linq;

...

/* your list of hardcoded employees */
list<object> listEmployees = your_list;

/* Sort the list by using linq and save it as sortedEmployees
   The Sorting is done based on the property ID */
list<object> sortedEmployees = listEmployees.OrderByDescending(t => t.ID);

/* set the datasource of your gridview */
GridView1.DataSource = sortedEmployees;

...
使用System.Linq;
...
/*您的硬编码员工列表*/
list listEmployees=您的_列表;
/*使用linq对列表进行排序,并将其另存为sortedEmployees
排序是基于属性ID完成的*/
list-sortedEmployees=listedemployees.OrderByDescending(t=>t.ID);
/*设置gridview的数据源*/
GridView1.DataSource=SortedDeEmployees;
...

通常,在将数据源分配给gridview之前,您会对数据源进行排序。您使用的是哪种数据源?目前只使用硬编码对象。如果您的硬编码对象是对象列表,我建议您首先对对象列表进行排序,然后将对象列表分配给gridview的数据源。您可以使用Linq对列表进行排序