title: Sort a BindingSource which is a set of objects by a series of properties
body: <p>The question has been asked a lot (which I am aware of) however I have been unable to implement something which works for me specifically.</p>
  
  <p>I am using Entity Framework in order to handle data interaction (and am somewhat new to the contept) with a WinForms application; this works fine and means that the <code>DataSource</code> of any DataGridView control is a list of objects.</p>
  
  <p>The user is to have the ability to sort on multiple columns. For DataGridView controls which have no <code>DataSource</code> (as they're one-way and I have pulled the data straight from the DB) this is not an issue, as a custom <code>IComparer</code> class handles this sorting.</p>
  
  <p>However, I appear to be unable to sort grids which have a <code>DataSource</code> where the <code>DataSource</code> is a set of objects by multiple columns.</p>
  
  <p>The important thing to observe is that the <code>DataSource</code> of the DataGridView could be a list of objects of any type (Customers, Recipes, Cars etc.) as the grid could be representing data from any table in the database through objects.</p>
  
  <p>The way I am getting the <code>DataSource</code> for the DataGridView control is as follows (this example is for the users in the system):</p>
  
  <pre><code>dtgUserList.pFormGrid.DataEntityProvider.USER_LIST
                      .Where(X =&gt; X.USER_ID &gt; 1)
                      .Load();
  
  dtgUserList.pFormGrid.DataSource = dtgUserList.pFormGrid.DataEntityProvider.USER_LIST.Local.ToBindingList();
  </code></pre>
  
  <p>When the <code>DataSource</code> of the DataGridView was a <code>DataTable</code> which was a direct database query, I could simply cast the <code>DataSource</code> of the DataGridView as a <code>BindingSource</code> and then set the <code>Sort</code> property to be the sorting string. For example, if sorting the user list by USER_NAME and then by PROFILE, the sort would be as follows:</p>
  
  <pre><code>((BindingSource)DataSource).Sort = SortArgs;
  </code></pre>
  
  <p>Where SortArgs = "USER_NAME ASC, PROFILE ASC". This could be determined simply by iterating through the columns which were sorted and extracting and concatenating the <code>DataPropertyName</code> of the column.</p>
  
  <p>If I try and cast the <code>DataSource</code> of the DataGridView control which is bound to the above set of <code>USER_LIST</code> objects however, I get a compile time error indicating that the cast cannot be performed.</p>
  
  <p>To try and address this, I am creating a new <code>BindingSource</code> from the existing <code>DataSource</code> in order to then try and apply the same sort logic (of just creating the sort string and then passing this to the <code>Sort</code> property of the <code>BindingSource</code>):</p>
  
  <pre><code>BindingSource GridBindingSource = new BindingSource(DataSource, null);
  SortedGridBinding.Sort = SortArgs;
  </code></pre>
  
  <p>Unfortunately, when I do this, the <code>SupportsAdvancedSorting</code> property is always <code>false</code>, despite the <code>BindingSource</code> implementing <code>IBindingListView</code>, which is documented in the <a href="https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.supportsadvancedsorting(v=vs.110).aspx" rel="nofollow" title="MSDN">MSDN</a> article for this property.</p>
  
  <p>What I am primarily trying to understand is why this property is always being set to <code>false</code>, despite the class I am using implementing the required interface to allow multi-column sorting. I have also seen a number of examples on how to implement sorting of lists of objects by multiple properties, such as <a href="http://www.codeproject.com/Articles/29219/Sorting-Collections-by-Multiple-Properties" rel="nofollow" title="this example">this example</a> however, unless I am mis-understanding something, this still relies on a strongly typed IList input.</p>