2010-03-31 43 views
14

Làm cách nào để tôi có thể bật tính năng phân loại BLL tự động của tôi trả về danh sách, Danh sách khách hàng: Danh sách trong GridView?Làm cách nào để bật Tự động sắp xếp dữ liệu IEnumerable trong GridView?

Khách hàng là lớp học được đánh máy mạnh mẽ của riêng tôi và Danh sách khách hàng là Danh sách khách hàng.

Tôi biết một cách tiếp cận là đặt thuộc tính AllowSorting thành true trong GridView và xử lý sự kiện OnSorting và gọi phương thức sắp xếp được xác định trong lớp CustomerList của tôi.

Tuy nhiên tôi muốn một giải pháp tự động theo nghĩa là tôi không phải xử lý sự kiện OnSorting, nó sẽ giống như cách GridView xử lý sắp xếp tự động cho DataView, DataTable và DataSet.

Có giao diện nào tôi cần triển khai trên Danh sách khách hàng hoặc Lớp khách hàng của mình để bật chức năng đó không?

alt text http://img260.imageshack.us/img260/3373/aa479347gridviewfg21enu.gif

+0

Rất nhiều thông tin tốt về sắp xếp GridView ở đây: http://msdn.microsoft.com/en-us/library/hwf94875(VS.80).aspx –

Trả lời

7

OK tôi đã tìm ra. Đây là giải pháp:

  1. Gắn BLL vào một ObjectDataSource.
  2. Cung cấp các phương thức quá tải cho phương thức chọn trong BLL của bạn, để hỗ trợ phân trang và phân loại.
  3. Cung cấp SortParameterName trong ObjectDataSource. Đây là tên của tham số đầu vào chuỗi của phương thức bạn chọn trong BLL của bạn.

Để biết thêm thông tin xem: http://msdn.microsoft.com/en-us/library/aa479347.aspx

Dưới đây là một ví dụ, đây chỉ là một ví dụ quck cho bản demo tôi không hỗ trợ định hướng sắp xếp, hoặc tối ưu hóa etc mã:

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


    } 
} 

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

Để biết thêm thông tin, xem: http://msdn.microsoft.com/en-us/library/aa479347.aspx

+0

Trong giải pháp ví dụ, tôi đang sử dụng một danh sách mảng để lưu các bản ghi DB và sau đó tiết kiệm arraylist đối tượng BLL của tôi, tôi có thể đã sử dụng thẳng đối tượng BLL để lưu các bản ghi DB nhưng trong tương lai tôi muốn di chuyển kết nối cơ sở dữ liệu sang một DAL, nó sẽ trả về một danh sách mảng cho BLL của tôi. – ace

0

Bạn có thể thực hiện cùng một logic trong DAL bằng cách lưu trữ sắp xếp và hướng trong phiên. Lấy Sortexpression n hướng từ GridView phương pháp phân loại và sắp xếp làm ở Dal sử dụng các paramters .Nhưng u hav để chăm sóc các ngoại lệ trong việc thiết e.cancel = true

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

cũng tham khảo http://forums.asp.net/t/1344883.aspx

Các vấn đề liên quan