<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Peter Ritchie's MVP Blog : Connect Issue</title><link>http://msmvps.com/blogs/peterritchie/archive/tags/Connect+Issue/default.aspx</link><description>Tags: Connect Issue</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Trials and Tribulations of DataGridView, Column Selections, and Sorting</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/08/26/trials-and-tribulations-of-datagridview-column-selections-and-sorting.aspx</link><pubDate>Tue, 26 Aug 2008 20:06:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1645891</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1645891</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/08/26/trials-and-tribulations-of-datagridview-column-selections-and-sorting.aspx#comments</comments><description>&lt;p&gt;I had to implement some &lt;em&gt;custom&lt;/em&gt; sorting in a &lt;span style="font-family:Courier New;"&gt;DataGridView&lt;/span&gt; recently.&amp;nbsp; Essentially, the stakeholders wanted full column selection (like Excel) while still having the ability to sort the data based on a particular column.&lt;/p&gt;
&lt;p&gt;This particular &lt;span style="font-family:Courier New;"&gt;DataGridView&lt;/span&gt; is data-bound.&amp;nbsp; &lt;span style="font-family:Courier New;"&gt;DataGridView&lt;/span&gt; offers the &lt;span style="font-family:courier new,courier;"&gt;Sort(DataGridViewColumn, ListSortDirection)&lt;/span&gt; method to perform this.&amp;nbsp; Nice and easy I thought: I&amp;rsquo;ll set the &lt;span style="font-family:Courier New;"&gt;SelectionMode&lt;/span&gt; to &lt;span style="font-family:Courier New;"&gt;DataGridViewSelectionMode.ColumnHeaderSelect&lt;/span&gt; and simply call &lt;span style="font-family:courier new,courier;"&gt;Sort&lt;/span&gt; with the selected column.&lt;/p&gt;
&lt;p&gt;Well, much to my chagrin this had the side effect of making that column look selected all the time.&amp;nbsp; No matter where else I clicked, that recently sorted column &lt;em&gt;looked&lt;/em&gt; selected (&lt;span style="font-family:Courier New;"&gt;SelectedColumns&lt;/span&gt; had a count of zero).&amp;nbsp; And to add insult to injury, when I control-clicked that column (thinking it was selected) to unselected it, it caused a &lt;span style="font-family:Courier New;"&gt;NullReferenceException&lt;/span&gt; deep in the framework.&lt;/p&gt;
&lt;p&gt;Suffice it to say, this makes it very difficult to sort by columns in &lt;span style="font-family:Courier New;"&gt;DataGridView&lt;/span&gt; without using the built-in sort-column-when-header-is-clicked mode.&lt;/p&gt;
&lt;p&gt;What I&amp;rsquo;m now attempting to do is to unselect the column before sorting it.&amp;nbsp; &lt;span style="text-decoration:line-through;"&gt;This, in itself, is not trivial either; there&amp;rsquo;s no public method to select or deselect a column in the &lt;span style="font-family:Courier New;"&gt;DataGridView&lt;/span&gt;.&amp;nbsp; I&amp;rsquo;ve had to create a new &lt;span style="font-family:Courier New;"&gt;DataGridView&lt;/span&gt; derivative and call the protected method &lt;span style="font-family:Courier New;"&gt;SetSelectedColumnCore&lt;/span&gt;.&amp;nbsp; A few hoops&amp;hellip;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve logged a couple of issues on Microsoft Connect about these problems.&amp;nbsp; The first is about ctrl-clicking the column and getting an exception:&amp;nbsp; &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363623" title="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363623"&gt;https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363623&lt;/a&gt; The second is about the visual state of the column remaining &amp;ldquo;selected&amp;rdquo;: &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363623"&gt;https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363623&lt;/a&gt; Attached to this post you will find the project referenced by the two Connect issues.&lt;/p&gt;
&lt;p&gt;[Update: I&amp;#39;ve currently only tried this with a .NET 2.0 project in Visual Studio 2008 SP1; if you find this problem occurs in Visual 2008 RTM, please comment.]&lt;/p&gt;
&lt;p&gt;[Update: I overlooked the DataGridViewColumn.Select property, so there&amp;#39;s no need to derive from &lt;span style="font-family:courier new,courier;"&gt;DataGridView&lt;/span&gt;]&lt;/p&gt;
&lt;div style="text-align:left;margin:0px;padding:4px 4px 4px 4px;" class="wlWriterHeaderFooter"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://msmvps.com/blogs/peterritchie/archive/2008/08/26/trials-and-tribulations-of-datagridview-column-selections-and-sorting.aspx&amp;amp;title=Trials%20and%20Tribulations%20of%20DataGridView,%20Column%20Selections,%20and%20Sorting"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://msmvps.com/blogs/peterritchie/archive/2008/08/26/trials-and-tribulations-of-datagridview-column-selections-and-sorting.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1645891" width="1" height="1"&gt;</description><enclosure url="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.01.64.58.91/DataGridViewBug.zip" length="22598" type="application/x-zip-compressed" /><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Framework+Bugs/default.aspx">Framework Bugs</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Connect+Issue/default.aspx">Connect Issue</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Visual+Studio+2008+SP1/default.aspx">Visual Studio 2008 SP1</category></item></channel></rss>