2014-05-14 18 views
10

Ho una funzione che restituisce un IList < T> ed è il DataSource per un DataGridView. Ho saputo che DataGridView non ordinerà IList. Ho letto This stackoverflow Q&A e sto cercando di implementare SortableBindingList. Devo fare qualcosa di sbagliato perché il mio DataGridView è vuoto. Ho anche provato ad accedere a un elemento da SortableBindingSource con un TextBox e anche niente.DataGridView utilizzando 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; 

    } 
} 

ho ispezionato la Microsoft.SqlServer.Management.Controls, fa questo guardare a destra?

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

Apprezzo molto l'aiuto e mi aiuta a imparare. Grazie a tutti!

+0

ho ottenuto questo per sviluppare la mia stessa classe SortableBindingList come nell'esempio StackOverflow. Volevo usare il Microsoft.SqlServer.Management.Controls.dll però. Qual è l'accordo? – waltmagic

risposta

16

Prova questa SortableBindingList:

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

Molto simile a quello che sto usando ora. Il tuo sembra un po 'più "snello" e genera NotSupportedException. Grazie per la risposta rapida! Perché Microsoft.SqlServer.Management.Controls.dll da C: \ Programmi \ Microsoft SQL Server \ 100 \ Setup Bootstrap \ Release \ x64 è vuoto e non funziona – waltmagic

+1

@waltmagic Volevo provare come funziona per me, ma ora ho problemi con i problemi di compatibilità della mia architettura di progetto e arhitecture del processore quando importare Microsoft.SqlServer.Management.Controls.dll e semplicemente non verrà eseguito :) – msmolcic

+0

Ricevo anche questo avviso. Installerò la versione a 32 bit di SQL Server o ruberò Microsoft.SqlServer.Management.Controls.dll da uno dei miei server e vedrò se questo fa la differenza. Grazie! – waltmagic