Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I recall having issues finding something that would work when I added sorting to my datagrids too. You can implement a sortable bindable list by first adding the following class to your project. It is a list implementation that implements <code>BindingList&lt;T&gt;</code>, so that you can bind your datagrid to it, and it also supports sorting. A better explanation of the details than I could give is on <a href="http://msdn.microsoft.com/en-us/library/aa480736.aspx" rel="noreferrer">MSDN here</a></p> <pre><code>public class SortableBindingList&lt;T&gt; : BindingList&lt;T&gt; { private ArrayList sortedList; private ArrayList unsortedItems; private bool isSortedValue; public SortableBindingList() { } public SortableBindingList(IList&lt;T&gt; 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 &lt; 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 &amp;&amp; prop.PropertyType.IsValueType) { Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType); if (underlyingType != null) { interfaceType = underlyingType.GetInterface("IComparable"); } } if (interfaceType != null) { sortPropertyValue = prop; sortDirectionValue = direction; IEnumerable&lt;T&gt; query = base.Items; if (direction == ListSortDirection.Ascending) { query = query.OrderBy(i =&gt; prop.GetValue(i)); } else { query = query.OrderByDescending(i =&gt; 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 &lt; unsortedItems.Count; ) { position = this.Find("LastName", unsortedItems[i].GetType(). GetProperty("LastName").GetValue(unsortedItems[i], null)); if (position &gt; 0 &amp;&amp; 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; } } } </code></pre> <p>With that in place, the only changes you need to make to the code that you have posted above is to create a SortableBindingList based on your list and bind to the sortable list, rather than the standard one, like so:</p> <pre><code>List&lt;MyClass&gt; list = new List&lt;MyClass&gt;(); list.Add(new MyClass("Peter", 1202)); list.Add(new MyClass("James", 292)); list.Add(new MyClass("Bond", 23)); // Added sortable list... SortableBindingList&lt;MyClass&gt; sortableList = new SortableBindingList&lt;MyClass&gt;(list); BindingSource bs = new BindingSource(); bs.DataSource = sortableList; // Bind to the sortable list </code></pre> <p>And that will be enough to get you going.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload