<?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 : Framework Bugs</title><link>http://msmvps.com/blogs/peterritchie/archive/tags/Framework+Bugs/default.aspx</link><description>Tags: Framework Bugs</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><item><title>Formatting/parsing for a specific culture redux</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/01/22/formatting-parsing-for-a-specific-culture-redux.aspx</link><pubDate>Tue, 22 Jan 2008 23:35:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1442456</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1442456</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/01/22/formatting-parsing-for-a-specific-culture-redux.aspx#comments</comments><description>&lt;p&gt;In &lt;a class="" href="http://msmvps.com/blogs/peterritchie/archive/2007/12/27/formatting-parsing-for-a-specific-culture.aspx"&gt;recent blog post&lt;/a&gt; I detailed how creating a culture via the CultureInfo constructor could actually create a user-overridden culture--which could be completely different than the culture that you&amp;#39;ve requested by name.&amp;nbsp; Fortunately there&amp;#39;s a way of overriding the user override (apologies for&amp;nbsp;overloading &amp;quot;override&amp;quot;) by supplying&amp;nbsp;the boolean value &amp;quot;false&amp;quot; in&amp;nbsp;a CultureInfo overload.&lt;/p&gt;
&lt;p&gt;As &lt;a class="" href="http://gregbeech.com/blogs/tech/"&gt;Greg Beech&lt;/a&gt; &lt;a class="" href="http://msmvps.com/blogs/peterritchie/archive/2007/12/27/formatting-parsing-for-a-specific-culture.aspx#1442156"&gt;commented&lt;/a&gt;, there&amp;#39;s another method to create a culture--&lt;a class="" href="http://msdn2.microsoft.com/en-us/library/system.globalization.cultureinfo.createspecificculture(VS.80).aspx"&gt;System.Globalization.CultureInfo.CreateSpecificCulture&lt;/a&gt;.&amp;nbsp; This sounds like it does exactly what you might expect and creates a &amp;quot;specific&amp;quot; culture.&amp;nbsp; Unfortunately, this method too violates the &lt;a class="" href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment"&gt;principle of least astonishment&lt;/a&gt; and creates a culture that uses the user-overridden values when the culture name matches that of the user&amp;#39;s current culture.&lt;/p&gt;
&lt;p&gt;CreateSpecificCulture does not, as far as I can tell, have an overload/alternative to you allow to to force a &amp;quot;specific&amp;quot; culture, so the problem is much worse with CreateSpecificCulture.&amp;nbsp; I&amp;#39;ve gone ahead and logged a bug for it on Connect: &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=321241"&gt;https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=321241&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In case you&amp;#39;re wondering why this is more serious, consider the following following block of code that formats a date value as text:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Lucida Console Modified;"&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Lucida Console Modified;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt; ci;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt; text;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ci = System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;en-us&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; text = &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt;.Format(ci, &lt;span style="COLOR:#a31515;"&gt;&amp;quot;{0:d}&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;DateTime&lt;/span&gt;.Now);&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And that text&amp;nbsp;is transmitted to another application (or another session of the same application, e.g. serialization), potentially in another locale, to be&amp;nbsp;parsed with the following&amp;nbsp;block of code:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Lucida Console Modified;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt; ci;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ci = System.Globalization.&lt;span style="COLOR:#2b91af;"&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;en-us&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt; text = ReadRecord();&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Lucida Console Modified;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;DateTime&lt;/span&gt; now = &lt;span style="COLOR:#2b91af;"&gt;DateTime&lt;/span&gt;.Parse(text, ci);&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If either (or) the user&amp;#39;s current culture is set to &amp;quot;English (United States)&amp;quot;&amp;nbsp;and they&amp;#39;ve overridden the currency format the short date (say from &amp;quot;M/d/yyyy&amp;quot; to &amp;quot;d/M/yyyy&amp;quot;) will randomly result in the wrong date.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1442456" width="1" height="1"&gt;</description><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/.NET+2.0/default.aspx">.NET 2.0</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/DevCenterPost/default.aspx">DevCenterPost</category></item></channel></rss>