2014-05-14 14 views
10

Tôi có hàm trả về một IList < T> và là DataSource cho một DataGridView. Tôi đã học được rằng DataGridView sẽ không sắp xếp IList. Tôi đọc This stackoverflow Q&A và đang cố triển khai SortableBindingList. Tôi phải làm điều gì sai vì DataGridView của tôi trống. Tôi cũng đã cố gắng truy cập một phần tử từ SortableBindingSource với một TextBox và không có gì là tốt.DataGridView Sử dụng SortableBindingList

using Microsoft.SqlServer.Management.Controls; 
public partial class Form1 : Form 
{ 
    IBusinessLayer businessLayer; 
    IList<Category> categories; 
    SortableBindingList<Category> catSortable; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

     businessLayer = new BusinessLayer(); 

     categories = businessLayer.GetAllCategories(); 
     catSortable = new SortableBindingList<Category>(categories); 
     categoryBindingSource.DataSource = catSortable; 
     categoryDataGridView.DataSource = categoryBindingSource; 

     textBox1.Text = catSortable[0].CategoryName; 

    } 
} 

Tôi kiểm tra các Microsoft.SqlServer.Management.Controls, điều này trông đúng không?

namespace Microsoft.SqlServer.Management.Controls 
{ 
    public class SortableBindingList<T> : BindingList<T> 
    { 
     public SortableBindingList(); 
     public SortableBindingList(IList<T> list); 

     protected override bool IsSortedCore { get; } 
     protected override ListSortDirection SortDirectionCore { get; } 
     protected override PropertyDescriptor SortPropertyCore { get; } 
     protected override bool SupportsSortingCore { get; } 

     protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction); 
     protected override void RemoveSortCore(); 
    } 
} 

Tôi thực sự đánh giá cao sự trợ giúp và giúp tôi tìm hiểu. Cảm ơn mọi người!

+0

tôi để làm việc này bằng cách tạo lớp SortableBindingList của riêng tôi như trong ví dụ stackoverflow. Tôi muốn sử dụng Microsoft.SqlServer.Management.Controls.dll mặc dù. Thỏa thuận là gì? – waltmagic

Trả lời

16

Hãy thử SortableBindingList này:

public class SortableBindingList<T> : BindingList<T> 
{ 
    private bool isSortedValue; 
    ListSortDirection sortDirectionValue; 
    PropertyDescriptor sortPropertyValue; 

    public SortableBindingList() 
    { 
    } 

    public SortableBindingList(IList<T> list) 
    { 
     foreach (object o in list) 
     { 
      this.Add((T)o); 
     } 
    } 

    protected override void ApplySortCore(PropertyDescriptor prop, 
     ListSortDirection direction) 
    { 
     Type interfaceType = prop.PropertyType.GetInterface("IComparable"); 

     if (interfaceType == null && prop.PropertyType.IsValueType) 
     { 
      Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType); 

      if (underlyingType != null) 
      { 
       interfaceType = underlyingType.GetInterface("IComparable"); 
      } 
     } 

     if (interfaceType != null) 
     { 
      sortPropertyValue = prop; 
      sortDirectionValue = direction; 

      IEnumerable<T> query = base.Items; 

      if (direction == ListSortDirection.Ascending) 
      { 
       query = query.OrderBy(i => prop.GetValue(i)); 
      } 
      else 
      { 
       query = query.OrderByDescending(i => prop.GetValue(i)); 
      } 

      int newIndex = 0; 
      foreach (object item in query) 
      { 
       this.Items[newIndex] = (T)item; 
       newIndex++; 
      } 

      isSortedValue = true; 
      this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); 
     } 
     else 
     { 
      throw new NotSupportedException("Cannot sort by " + prop.Name + 
       ". This" + prop.PropertyType.ToString() + 
       " does not implement IComparable"); 
     } 
    } 

    protected override PropertyDescriptor SortPropertyCore 
    { 
     get { return sortPropertyValue; } 
    } 

    protected override ListSortDirection SortDirectionCore 
    { 
     get { return sortDirectionValue; } 
    } 

    protected override bool SupportsSortingCore 
    { 
     get { return true; } 
    } 

    protected override bool IsSortedCore 
    { 
     get { return isSortedValue; } 
    } 
} 
+0

Rất giống với cái tôi đang sử dụng bây giờ. Của bạn có vẻ hơi "mỏng" hơn và ném NotSupportedException. Cảm ơn đã trả lời nhanh chóng! Tại sao Microsoft.SqlServer.Management.Controls.dll từ C: \ Program Files \ Microsoft SQL Server \ 100 \ Setup Bootstrap \ Release \ x64 trống và không hoạt động – waltmagic

+1

@waltmagic Tôi muốn thử cách nó hoạt động cho tôi, nhưng bây giờ tôi đang gặp vấn đề với các vấn đề tương thích của kiến ​​trúc dự án và kiến ​​trúc bộ xử lý của tôi khi tôi nhập Microsoft.SqlServer.Management.Controls.dll và nó sẽ không chạy :) – msmolcic

+0

Tôi cũng nhận được cảnh báo đó. Tôi sẽ cài đặt phiên bản 32 bit của SQL Server hoặc lấy cắp Microsoft.SqlServer.Management.Controls.dll từ một trong các máy chủ của tôi và xem điều đó có tạo ra sự khác biệt hay không. Cảm ơn! – waltmagic

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