lundi 26 janvier 2015

Programmatically sorting my SortableBindingList


Vote count:

0




I have a class that implements a sortable binding list:



public class MySortableBindingList_C<T> : BindingList<T>, IBindingListView, IBindingList, IList, ICollection, IEnumerable


It works just fine in a data grid view, this successfully sorts the list:



public Form1(MySortableBindingList_C<Widget_C> sortable)
{
InitializeComponent();
dataGridView1.DataSource = sortable;
dataGridView1.Sort(dataGridView1.Columns["Id"], ListSortDirection.Ascending);
this.Close();
}


But how do I sort that without the use of DataGridView?


Things I have tried:



MySortableBindingList_C<Widget_C> sortable = new MySortableBindingList_C<Widget_C>();
sortable.Add(new Widget_C { Id = 5, Name = "Five" });
sortable.Add(new Widget_C { Id = 3, Name = "Three" });
sortable.Add(new Widget_C { Id = 2, Name = "Two" });
sortable.Add(new Widget_C { Id = 4, Name = "Four" });
sortable.OrderBy(w=> w.Id); // sorts, but produces a new sorted result, does not sort original list
sortable.ApplySort(new ListSortDescriptionCollection({ new ListSortDescription(new PropertyDescriptor(), ListSortDirection.Ascending))); // PropertyDescriptor is abstract, does not compile
typeof(Widget_C).GetProperty("Id"); // This gets a PropertyInfo, not a PropertyDescriptor


asked 33 secs ago







Programmatically sorting my SortableBindingList

Aucun commentaire:

Enregistrer un commentaire