<?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>Trying to walk that thin line between intelligence and ignorance</description>
	<lastBuildDate>Thu, 29 Jul 2010 20:51:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</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&#8217; 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&#8217; 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;">&#8221; DESC&#8221;</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&#8230; only one column at a time, sorting can&#8217;t be removed, etc&#8230; For a lot of scenario&#8217;s though, this could already be sufficient</p>
]]></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>
