Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to be able to sort the data automatically in the <code>DataGridView</code>, you need a collection that implements <code>IBindingListView</code>. In the BCL, the only classes that implement this interface are <code>DataView</code> and <code>BindingSource</code> (but the latter only supports sorting if the underlying datasource supports it too).</p> <p>So, you have several options:</p> <ul> <li>create a <code>DataTable</code> to hold the data, and bind it to the <code>DataGridView</code> (it will actually bind to the <code>DefaultView</code> of the <code>DataTable</code>)</li> <li>create your own collection class that implements <code>IBindingListView</code></li> <li><p>use an existing implementation, like the <code>AdvancedList&lt;T&gt;</code> class posted by Marc Gravell in <a href="http://www.eggheadcafe.com/software/aspnet/31429914/sort-a-bindinglist.aspx" rel="noreferrer">this post</a>. You will also need to add a constructor to build the list from the result of your query:</p> <pre><code>public AdvancedList(IEnumerable&lt;T&gt; collection) { foreach (var item in collection) { Add(item); } } </code></pre></li> </ul> <p>Since the result of your query is an anonymous type, you won't be able to call the constructor directly. The easiest way to work around the issue is to take advantage of type inference, by creating a generic method that will create the list. For convenience, you can create it as an extension method:</p> <pre><code>public static AdvancedList&lt;T&gt; ToAdvancedList&lt;T&gt;(this IEnumerable&lt;T&gt; source) { return new AdvancedList&lt;T&gt;(source); } </code></pre> <p>You can then use it like that:</p> <pre><code>dataGridView1.DataSource = _markets.Select(market =&gt; new { _cities[market.Location].Name, market.Value}).ToAdvancedList(); </code></pre>
    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