Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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中启用IEnumerable数据的自动排序?_C#_Asp.net_Gridview_Sorting_Ienumerable - Fatal编程技术网

C# 如何在GridView中启用IEnumerable数据的自动排序?

C# 如何在GridView中启用IEnumerable数据的自动排序?,c#,asp.net,gridview,sorting,ienumerable,C#,Asp.net,Gridview,Sorting,Ienumerable,如何在GridView中对返回列表CustomerList:list的BLL启用自动排序 Customer是我自己的强类型类,CustomerList是客户列表 我知道一种方法是在GridView中将AllowSorting属性设置为true,处理OnSorting事件并调用CustomerList类中定义的排序方法 然而,我希望有一个解决方案是自动的,因为我不必处理OnSorting事件,它应该像GridView处理DataView、DataTable和DataSet的自动排序一样 我是否需要

如何在GridView中对返回列表CustomerList:list的BLL启用自动排序

Customer是我自己的强类型类,CustomerList是客户列表

我知道一种方法是在GridView中将AllowSorting属性设置为true,处理OnSorting事件并调用CustomerList类中定义的排序方法

然而,我希望有一个解决方案是自动的,因为我不必处理OnSorting事件,它应该像GridView处理DataView、DataTable和DataSet的自动排序一样

我是否需要在CustomerList或Customer类上实现一个接口来启用该功能


好吧,我想出来了。以下是解决方案:

  • 将BLL绑定到ObjectDataSource
  • 为BLL中的select方法提供重载方法,以支持分页和排序
  • 在ObjectDataSource中提供SortParameterName。这是BLL中select方法的字符串输入参数的名称
  • 有关更多信息,请参阅:

    下面是一个示例,这只是一个演示的quck示例,我不支持排序方向,也不优化代码等:

    namespace CodeSamples.DAL
    {
        public static class DAL
        {
            public static CustomerList GetCustomerList(string SortExpression)
            {
                return GetCustomerList(int.MaxValue, 0, SortExpression);
            }
    
            public static CustomerList GetCustomerList()
            {
                return GetCustomerList(int.MaxValue, 0,String.Empty);
            }
    
            public static CustomerList GetCustomerList(int maximumRows, int startRowIndex, string SortExpression)
            {
                const string query = "Select * from Customers";
                CustomerList customers = new CustomerList();
    
    
                SqlConnection conn = new SqlConnection("Data Source=Win2k8;Initial Catalog=NorthWind;User ID=sa;Password=XXXXX");
                SqlCommand command = new SqlCommand(query, conn);
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
    
                ArrayList rows = new ArrayList();
    
                while (reader.Read())
                {
                    object[] values = new object[reader.FieldCount];
                    reader.GetValues(values);
                    rows.Add(values);
                }
    
                conn.Close();
    
                int currentIndex = 0;
                int itemsRead = 0;
                int totalRecords = rows.Count;
    
                foreach (object[] row in rows)
                {
                    if (currentIndex >= startRowIndex && itemsRead <= maximumRows)
                    {
                        customers.Add(new Customer { Name = row[1].ToString(), ID = row[0].ToString(), ContactName = row[2].ToString() });
                        itemsRead++;
                    }
                    currentIndex++;
                }
    
    
            CustomerList sortedCustomers = new CustomerList();
    
            string sortBy = SortExpression;
            bool isDescending = false;
    
            if (SortExpression.ToLowerInvariant().EndsWith(" desc"))
            {
                sortBy = SortExpression.Substring(0, SortExpression.Length - 5);
                isDescending = true;
            }         
    
            var sortedList = from customer in customers
                             select customer;
    
            switch (sortBy)
            {
                case "ID":
                    sortedList = isDescending ? sortedList.OrderByDescending(cust => cust.ID) : sortedList.OrderBy(cust => cust.ID);
                    break;
    
                case "Name":
                    sortedList = isDescending ? sortedList.OrderByDescending(cust => cust.Name) : sortedList.OrderBy(cust => cust.Name);
                    break;
    
                case "ContactName":
                    sortedList = isDescending ? sortedList.OrderByDescending(cust => cust.ContactName) : sortedList.OrderBy(cust => cust.ContactName);
                    break;
    
            }
    
            foreach (Customer x in sortedList)
            {
                sortedCustomers.Add(x);
            }    
    
                return sortedCustomers;
            }
        }  
    
        public class CustomerList : List<Customer>
        {
    
        } 
    
        public class Customer
        {
            public Customer()
            {
            }
    
            public Customer(string Name, string id)
            {
                this.Name = Name;
                ID = id;
            }
            public string ID
            {
                get;
                set;
            }
    
            public string Name
            {
                get;
                set;
            }
    
            public string ContactName
            {
                get;
                set;
            }
    
    
        }
    }
    
    namespace CodeSamples.DAL
    {
    公共静态类DAL
    {
    公共静态CustomerList GetCustomerList(字符串排序表达式)
    {
    返回GetCustomerList(int.MaxValue,0,SortExpression);
    }
    公共静态CustomerList GetCustomerList()
    {
    返回GetCustomerList(int.MaxValue,0,String.Empty);
    }
    公共静态CustomerList GetCustomerList(int maximumRows、int startRowIndex、string-SortExpression)
    {
    const string query=“选择*来自客户”;
    CustomerList customers=新CustomerList();
    SqlConnection conn=newsqlconnection(“数据源=Win2k8;初始目录=NorthWind;用户ID=sa;密码=XXXXX”);
    SqlCommand=newsqlcommand(查询,连接);
    conn.Open();
    SqlDataReader=command.ExecuteReader();
    ArrayList行=新的ArrayList();
    while(reader.Read())
    {
    object[]value=新对象[reader.FieldCount];
    reader.GetValues(值);
    行。添加(值);
    }
    康涅狄格州关闭();
    int currentIndex=0;
    int itemsRead=0;
    int totalRecords=行数;
    foreach(对象[]行中的行)
    {
    if(currentIndex>=startRowIndex&&itemsRead cust.ID):sortedList.OrderBy(cust=>cust.ID);
    打破
    案例“名称”:
    sortedList=isDescending?sortedList.OrderByDescending(cust=>cust.Name):sortedList.OrderBy(cust=>cust.Name);
    打破
    案例“ContactName”:
    sortedList=isDescending?sortedList.OrderByDescending(cust=>cust.ContactName):sortedList.OrderBy(cust=>cust.ContactName);
    打破
    }
    foreach(分拣列表中的客户x)
    {
    分类客户。添加(x);
    }    
    客户退货;
    }
    }  
    公共类CustomerList:列表
    {
    } 
    公共类客户
    {
    公众客户()
    {
    }
    公共客户(字符串名称、字符串id)
    {
    this.Name=Name;
    ID=ID;
    }
    公共字符串ID
    {
    得到;
    设置
    }
    公共字符串名
    {
    得到;
    设置
    }
    公共字符串联系人名称
    {
    得到;
    设置
    }
    }
    }
    
    在ASPX页面中:

      <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" 
                AllowSorting="True">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
                </Columns>
            </asp:GridView>
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                SelectMethod="GetCustomerList" SortParameterName="SortExpression"
                TypeName="CodeSamples.DAL.DAL">
            </asp:ObjectDataSource>
    

    通过在会话中存储sortexpression和direction,您可以在DAL中执行相同的逻辑。 从gridview排序方法获取Sortexpression n direction,并使用这些参数在DAL中进行排序。但在设置e.cancel=true时,必须注意异常

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
            {
         \\ Take sortexpression n direction
           e.cancel = true 
            }
    
    也指

    这里有很多关于GridView排序的好信息:在示例解决方案中,我使用arraylist保存DB记录,然后将arraylist保存到我的BLL对象,我可以直接使用BLL对象保存DB记录,但将来我想将数据库连接移动到DAL,它将向我的BLL返回一个数组列表。