Thursday, July 21, 2005

Couple of things that I noticed today

2 quick things.

The first is something that's caught me out a couple of times, so I'm committing it to writing so I'll finally remember:

Using a DateTime field in the RowFilter of a Dataset
Since the RowFilter property is a string, it doesn't lend itself terribly well to filtering with DateTime values. Or at least the default DateTime.ToString format doesn't lend itself to being put in a RowFilter property. Picture the scene. You want to filter the DataView for your datagrid on today's date. You'd thing you could do it with:

theView.RowFilter = String.Format("DateField = #{0}#", DateTime.Today

But you'd be wrong. You might even think you'd be able to do it with the ToShortDateString method:

theView.RowFilter = String.Format("DateField = #{0}#", DateTime.Today.ToShortDateString()

And (and here's the rub) in some parts of the world, you'd be right. The ToShortDateString method is sensitive to the culture of the box it's running on. The RowFilter property, however, isn't. So, if you're in 'Merica, your ToShortDateString method produces this string for 23rd October 2004: "10/23/2005". If you're on t'other side of the pond (or anywhere else that formats its short dates properly) ToShortDateString produces: "23/10/2004".

Now because the RowFilter property isn't sensitive to the regional settings of the OS, it slings out the second string as an invalid date. The way to filter on dates, if you're in one of the enlightened countries that formats dates as they were meant to be formatted is thus:

theView.RowFilter = String.Format("DateField = #{0}#", DateTime.Today("MM/dd/yyyy")

The second thing is just something that I wrote today that made me feel good.

Possibly the most elegant peice of code I've ever seen
I like elegant code. I especially like it when elegant code is also (I think) efficient code. It's down the the DataView's RowFilter property again, and it uses my favourite String.Format function. It just looks truly beautiful, though:

theView.RowFilter = String.Format("Name in ('{0}')", String.Join(, "', '", StringArrayOfNames))

I just think it looks really neat. Of course, the String.Format method returns "Name in ('Tom', 'Dick', 'Harry')", but it's the simplicity and elegance of the String.Join function and putting the placeholder inthe format string ({0}) in quotes that just makes me think that everything's alright in the world.

You may have noticed I've had a good day. Just wanted to share some of the joy.

2 comments:

Anonymous said...

If you're on t'other side of the pond (or anywhere else that formats its short dates properly) ToShortDateString produces: "23/10/2004".

Here is some output from just a handful of .NET cultures:

English (United Kingdom):
Full: 10 July 2007 00:00
Long: 10 July 2007
Short: 10/07/2007
English (United States):
Full: Tuesday, July 10, 2007 12:00 AM
Long: Tuesday, July 10, 2007
Short: 7/10/2007
français (Canada):
Full: 10 juillet 2007 00:00
Long: 10 juillet 2007
Short: 2007-07-10
Deutsch (Deutschland):
Full: Dienstag, 10. Juli 2007 00:00
Long: Dienstag, 10. Juli 2007
Short: 10.07.2007
русский (Россия):
Full: 10 июля 2007 г. 0:00
Long: 10 июля 2007 г.
Short: 10.07.2007
Español (Ecuador):
Full: martes, 10 de julio de 2007 0:00
Long: martes, 10 de julio de 2007
Short: 10/07/2007

Instead of thinking that only one way is correct for a culture you happen to be part of, maybe a solution that is culture insensitive would be a better posting.

Ex:
Me.dtmMyDate.ToString("d", New CultureInfo("en-US")) gives you the necessary string needed for your RowFilter.

Note: Change the culture to the needed one.

Anonymous said...

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. http://psiprograms.com