Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I sort an ObservableCollection<T> that's bound to a ListBoxControl?
    primarykey
    data
    text
    <p>I've got a winform app that uses 2 DevExpress ListBoxControls that a user can move items from one to the other. The box on the left has "All available" Sections while the box on the right has what's currently assigned to the user. I am new to Generics but am trying to implement them in this project. Each ListBoxControl is bound to an ObservableCollection via it's DataSource:</p> <pre><code>ObservableCollection&lt;Sections&gt; allSections = new ObservableCollection&lt;Sections&gt;((dbContext.Sections .OrderBy(s =&gt; s.SectionName).AsEnumerable&lt;Sections&gt;())); listboxAllSections.DataSource = allSections; listboxAllSections.DisplayMember = "SectionName"; listboxAllSections.ValueMember = "SectionID"; </code></pre> <p>So I have 4 buttons in between each listbox to allow the user to move items back and forth:</p> <p>MoveRight <code>&gt;</code><br> MoveAllRight <code>&gt;&gt;</code><br> MoveLeft <code>&lt;</code><br> MoveAllLeft <code>&lt;&lt;</code></p> <p>For <code>MoveRight</code> and <code>MoveLeft</code> I created this Generic function:</p> <pre><code>private void MoveItem&lt;T&gt;(ListBoxControl source, ListBoxControl target) { ObservableCollection&lt;T&gt; sourceItems = (ObservableCollection&lt;T&gt;)source.DataSource; ObservableCollection&lt;T&gt; targetItems = (ObservableCollection&lt;T&gt;)target.DataSource; target.BeginUpdate(); //Add items to target. foreach (T item in source.SelectedItems) targetItems.Add(item); //Remove items from source. for (int x = 0; x &lt; source.SelectedItems.Count; x++) sourceItems.Remove((T)source.SelectedItems[x]); target.EndUpdate(); } </code></pre> <p>All works great, but I would like to add sorting to it. I don't need sorting when moving items to the right, only when moving items to the left. I can't figure out how to sort a generic collection based on a certain property. For instance, <code>Sections</code> has a property called <code>SectionName</code> that I'd like to sort on. I can do this if I use the actual type:</p> <pre><code>ObservableCollection&lt;Sections&gt; sourceItems = (ObservableCollection&lt;Sections&gt;)listboxAllSections.DataSource; var sortedItems = new ObservableCollection&lt;Sections&gt;(); foreach (var item in sourceItems.OrderBy(t =&gt; t.SectionName)) sortedItems.Add(item); listboxAllSections.DataSource = sortedItems; </code></pre> <p>But I don't know how to make this generic to that <code>sourceItems.OrderBy(t =&gt; t.SectionName)</code> can be the field of the Type that's passed in.</p> <p>Any help/guidance is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. 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