<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>The Inquisitive Coder - Davy Brion&#039;s Blog &#187; WinForms</title> <atom:link href="http://davybrion.com/blog/category/winforms/feed/" rel="self" type="application/rss+xml" /><link>http://davybrion.com/blog</link> <description>inquisitive: adjective. given to inquiry, research, or asking questions; eager for knowledge; intellectually curious</description> <lastBuildDate>Sun, 29 Jan 2012 10:48:12 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Creating a sortable BindingList</title><link>http://davybrion.com/blog/2007/09/creating-a-sortable-bindinglist/</link> <comments>http://davybrion.com/blog/2007/09/creating-a-sortable-bindinglist/#comments</comments> <pubDate>Mon, 10 Sep 2007 11:19:49 +0000</pubDate> <dc:creator>Davy Brion</dc:creator> <category><![CDATA[WinForms]]></category><guid
isPermaLink="false">http://ralinx.wordpress.com/2007/09/10/creating-a-sortable-bindinglist/</guid> <description><![CDATA[Suppose you want to bind a collection of your custom objects to a DataGridView in a WinForm, and you want it to be sortable without having to write a lot of code. Once again, using Marc Brooks' DynamicComparer this becomes an easy task. Especially when you combine it with the BindingList class (which is a [...]]]></description> <content:encoded><![CDATA[<p>Suppose you want to bind a collection of your custom objects to a DataGridView in a WinForm, and you want it to be sortable without having to write a lot of code. Once again, using Marc Brooks' DynamicComparer this becomes an easy task.  Especially when you combine it with the BindingList class (which is a default implementation of the IBindingList interface):</p><div
style="font-family:Consolas;font-size:10pt;color:black;background:white;"><p
style="margin:0;">&nbsp;&nbsp;&nbsp; <span
style="color:blue;">public</span> <span
style="color:blue;">class</span> <span
style="color:teal;">SortableBindingList</span>&lt;T&gt; : <span
style="color:teal;">BindingList</span>&lt;T&gt;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; {</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:blue;">private</span> <span
style="color:teal;">List</span>&lt;T&gt; _sortableList;</p><p
style="margin:0;">&nbsp;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:blue;">public</span> SortableBindingList(<span
style="color:teal;">List</span>&lt;T&gt; list)</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; : <span
style="color:blue;">base</span>(list)</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _sortableList = list;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p><p
style="margin:0;">&nbsp;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:blue;">protected</span> <span
style="color:blue;">override</span> <span
style="color:blue;">bool</span> SupportsSortingCore</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:blue;">get</span> { <span
style="color:blue;">return</span> <span
style="color:blue;">true</span>; }</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p><p
style="margin:0;">&nbsp;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:blue;">protected</span> <span
style="color:blue;">override</span> <span
style="color:blue;">void</span> ApplySortCore(<span
style="color:teal;">PropertyDescriptor</span> property,</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <span
style="color:teal;">ListSortDirection</span> direction)</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:blue;">string</span> sortExpression = property.Name;</p><p
style="margin:0;">&nbsp;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:blue;">if</span> (direction == <span
style="color:teal;">ListSortDirection</span>.Descending)</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sortExpression = sortExpression + <span
style="color:maroon;">" DESC"</span>;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p><p
style="margin:0;">&nbsp;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:teal;">DynamicComparer</span>&lt;T&gt; comparer =</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:blue;">new</span> <span
style="color:teal;">DynamicComparer</span>&lt;T&gt;(sortExpression);</p><p
style="margin:0;">&nbsp;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _sortableList.Sort(comparer.Comparer);</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p><p
style="margin:0;">&nbsp;</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span
style="color:blue;">protected</span> <span
style="color:blue;">override</span> <span
style="color:blue;">void</span> RemoveSortCore() {}</p><p
style="margin:0;">&nbsp;&nbsp;&nbsp; }</p></div><p>Binding a sortable list to a DataGridView is now as easy as this:</p><div
style="font-family:Consolas;font-size:10pt;color:black;background:white;"><p
style="margin:0;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dataGridView.DataSource = <span
style="color:blue;">new</span> <span
style="color:teal;">SortableBindingList</span>&lt;<span
style="color:teal;">Order</span>&gt;(<span
style="color:teal;">OrderService</span>.GetAllOrders());</p></div><p>Obviously, the sorting capabilities are pretty basic... only one column at a time, sorting can't be removed, etc... For a lot of scenario's though, this could already be sufficient</p><div
class="bottomcontainerBox" style=""><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavybrion.com%2Fblog%2F2007%2F09%2Fcreating-a-sortable-bindinglist%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div><div
style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <g:plusone size="medium" href="http://davybrion.com/blog/2007/09/creating-a-sortable-bindinglist/"></g:plusone></div><div
style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://davybrion.com/blog/2007/09/creating-a-sortable-bindinglist/"  data-text="Creating a sortable BindingList" data-count="horizontal" data-via="davybrion">Tweet</a></div><div
style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://davybrion.com/blog/2007/09/creating-a-sortable-bindinglist/" data-counter="right"></script></div><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://davybrion.com/blog/2007/09/creating-a-sortable-bindinglist/"></script></div></div><div
style="clear:both"></div><div
style="padding-bottom:4px;"></div>]]></content:encoded> <wfw:commentRss>http://davybrion.com/blog/2007/09/creating-a-sortable-bindinglist/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/11 queries in 0.003 seconds using disk: basic
Object Caching 360/369 objects using disk: basic
Content Delivery Network via Amazon Web Services: CloudFront: d18sni7re4ly7f.cloudfront.net

Served from: davybrion.com @ 2012-02-08 04:57:35 -->
