Thursday, June 7, 2012

Telerik RadGridView Column Format Calamity

Wow... just wow. I lost about an hour trying to figure out a:
TargetInvocationException: Exception has been thrown by the target of invocation.

This is probably one of the most useless exception messages among others. It really tells you nothing more than, "Something really bad happened when you just did what you did." In my case this exception kept killing my program entirely. I found out where it was happening after searching high and low for the problem thinking it was my code causing the error. Turns out it had absolutely nothing to do with my code and everything to do with a Telerik control I am using called "RadGridView" which is essentially their version of DataGridView. I like it better than the stock control, but holy crap this was a big mind #&$% just now for no reason.

Here is why the error occurred:
I used the wrong type of format string for a Column that contains a DateTime object. This apparently causes the thread the control is running on to fall to its knees and die. Wow. And it isn't obvious either, this didn't happen when I didn't use the correct format string for other columns, it would just show me the string I provided - harmless.

Don't do this:

If you put the wrong or malformed format string in the FormatString property you will get the following exception during runtime after setting the DataSource property in your code:

This exception is going in the hall of shame because it doesn't tell you anything. USELESS!

Well it turns out the right way to format anything in Telerik controls is by using the following format: {0:FormatString} which is very similar to what you would do when formatting a string in the string.Format() method.

This is one hell of a gotcha, I should probably submit a bug report to Telerik because this truly is a bug.

No comments:

Post a Comment