2009-11-09 31 views
10

Bu konuda birçok soru olduğunu biliyorum. Hepsinden geçtim ama hiçbir şey yardım etmiyor.DataGridView sütunu nasıl sıralanır?

Sütun başlığına tıklayarak nasıl sıralanır?

İşi yapmak için bu kodu nasıl değiştirmeliyim?

public partial class Form1 : Form 
{ 

    public Form1() 
    { 

     List<MyClass> list = new List<MyClass>(); 
     list.Add(new MyClass("Peter", 1202)); 
     list.Add(new MyClass("James", 292)); 
     list.Add(new MyClass("Bond", 23)); 

     BindingSource bs = new BindingSource(); 
     bs.DataSource = list; 

     DataGridView dg = new DataGridView(); 

     DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn(); 
     c.Name = "name"; 
     c.DataPropertyName = "Name"; 
     dg.Columns.Add(c); 

     c = new DataGridViewTextBoxColumn(); 
     c.Name = "number"; 
     c.DataPropertyName = "Number"; 
     dg.Columns.Add(c); 

     dg.DataSource = bs; 

     this.Controls.Add((Control)dg); 

    } 

} 

class MyClass:IComparable<MyClass> 
{ 
    public string Name { get; set; } 
    public int Number { get; set; } 

    public MyClass(){} 

    public MyClass(string name,int number) 
    { 
     Name = name; 
     Number = number; 
    } 

    public override string ToString() 
    { 
     return string.Format("{0}:{1}",Name,Number); 
    } 

    #region IComparable<MyClass> Members 

    public int CompareTo(MyClass other) 
    { 
     return Name.CompareTo(other.Name); 
    } 

    #endregion 
} 

cevap

14

Veri kümelerime de sıralama eklediğimde işe yarayacak bir şey bulmaya çalışırken sorun yaşıyorum. Projenize ilk olarak aşağıdaki sınıfı ekleyerek sıralanabilir bir okunabilir liste uygulayabilirsiniz. BindingList<T>'u uygulayan bir liste uygulamasıdır, böylece veri kılavuzunuzu buna bağlayabilirsiniz ve aynı zamanda sıralama özelliğini de destekler. Ben verebilir daha detayların daha iyi bir açıklama yerinde önüne aldığımızda MSDN here

public class SortableBindingList<T> : BindingList<T> 
{ 
    private ArrayList sortedList; 
    private ArrayList unsortedItems; 
    private bool isSortedValue; 

public SortableBindingList() 
{ 
} 

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

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

protected override int FindCore(PropertyDescriptor prop, object key) 
{ 
    PropertyInfo propInfo = typeof(T).GetProperty(prop.Name); 
    T item; 

    if (key != null) 
    { 
     for (int i = 0; i < Count; ++i) 
     { 
      item = (T)Items[i]; 
      if (propInfo.GetValue(item, null).Equals(key)) 
       return i; 
     } 
    } 
    return -1; 
} 

public int Find(string property, object key) 
{ 
    PropertyDescriptorCollection properties = 
     TypeDescriptor.GetProperties(typeof(T)); 
    PropertyDescriptor prop = properties.Find(property, true); 

    if (prop == null) 
     return -1; 
    else 
     return FindCore(prop, key); 
} 

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


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

ListSortDirection sortDirectionValue; 
PropertyDescriptor sortPropertyValue; 

protected override void ApplySortCore(PropertyDescriptor prop, 
    ListSortDirection direction) 
{ 
    sortedList = new ArrayList(); 

    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 void RemoveSortCore() 
{ 
    int position; 
    object temp; 

    if (unsortedItems != null) 
    { 
     for (int i = 0; i < unsortedItems.Count;) 
     { 
      position = this.Find("LastName", 
       unsortedItems[i].GetType(). 
       GetProperty("LastName").GetValue(unsortedItems[i], null)); 
      if (position > 0 && position != i) 
      { 
       temp = this[i]; 
       this[i] = this[position]; 
       this[position] = (T)temp; 
       i++; 
      } 
      else if (position == i) 
       i++; 
      else 
       unsortedItems.RemoveAt(i); 
     } 
     isSortedValue = false; 
     OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); 
    } 
} 

public void RemoveSort() 
{ 
    RemoveSortCore(); 
} 
protected override PropertyDescriptor SortPropertyCore 
{ 
    get { return sortPropertyValue; } 
} 

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

} 

üzerindedir, size yukarıda gönderdiniz koduna yapmak gereken tek değişiklik listeye dayalı bir SortableBindingList yaratmaktır ve

List<MyClass> list = new List<MyClass>(); 
list.Add(new MyClass("Peter", 1202)); 
list.Add(new MyClass("James", 292)); 
list.Add(new MyClass("Bond", 23)); 

// Added sortable list... 
SortableBindingList<MyClass> sortableList = new SortableBindingList<MyClass>(list); 

BindingSource bs = new BindingSource(); 
bs.DataSource = sortableList; // Bind to the sortable list 

Ve bu gidiş almak için yeterli olacaktır: şöyle yerine standart olandan, sıralanabilir listeye bağlamak.

+0

bunu güzel !!! SortableBindingList uygulayan tamamen işlevsel bir projeye bağlantı için (+ 1A) –

4

İşte gerçekten bana gerçekten yardımcı olan blog yazısı.

Presenting the SortableBindableList

Ayrıca, bu ve başka kütüphane örnekleri vardır How do I implement automatic sorting of DataGridView? kontrol edin.

+1

+1 - kendi projenize 2 dosya bırakın. Yazarın kodu biraz güncellediğini unutmayın, http://www.timvw.be/presenting-the-sortablebindinglistt-take-two/ - sadece zip dosyasını indir –

+1

İlk bağlantı artık geçerli değil ... – Grahamvs