I have a asp:BoundColumn with DateTime? data (i'll call it column X) in the asp:GridView. I loop through all the rows in the grid, and cast the .Text property to column X to DateTime (i need this to do some checking and change the cell background color if needed).
I have 2 options:
wrap the cast in try/catch ... handle FormatException (in case column X gets rendered with some junk in it) ... and work only with valid DateTime values.
check the column X to make sure it's in the correct format (how?) before casing, and only cast if format is ok.
Which approach should i take, and why?
Thank you.
p.s. 1 up-vote for all explanations, accepted answer for TryParse
Edit2: even though i used TryParse, I've unchecked the accepted answer to keep the discussion going.
My "good data"/"bad data" ratio is about 2/1 - this means lots of bad input
I'll re-accept the answer in a couple of days, if nothing else comes up.