Problem displaying datetime column in DataGridView using SmartDate

Problem displaying datetime column in DataGridView using SmartDate

Old forum URL: forums.lhotka.net/forums/t/3963.aspx


yimiqi posted on Thursday, November 29, 2007

I have a read only list BO "AList" that contains a list of read only BO "A". 

A DataGridView DGV is binded to AList via databindingsource.

In object A, there is a property prop is SmartDate.  Its value is set by a datetime field in the database.

My problem is when AList is displayed in the DGV, the smartdate column only displays the date, but no time even if the datetime field in the database contains both date and time, and the SmartDate property in object A also has time in its date, DBValue fields, but not in its Text filed.

The datetime column in the DGV is a DataGridViewTextColumn, how can I display both date and time for a SmartDate in a DataGridViewTextColumn?

Thank you in advance.

RockfordLhotka replied on Thursday, November 29, 2007

Is the property itself a SmartDate? SmartDate is designed to be the backing field type for a property, and the property itself should be of type string.

In that case you can control the format string used to convert the SmartDate value to a text representation - there's a property on SmartDate that let's you set the .NET format string.

yimiqi replied on Friday, November 30, 2007

Thanks for your reply Rocky.  You brought up another question about usage of SamrtDate I was going to ask here.

To answer your question first, I have the property itself a SmartDate now, but it was a type of string (SmartDate.text).  I changed the property type from a string to a SmartDate.  The reason being was to be able to sort on the datetime column in the DataGridView correctly, meaning sort the date column as date not as string. 

e.g. Sort as String:  1/2/07,11/2/07,2/3/06

vs. Sort as date:  2/3/06, 1/2/07,11/2/07

My another question would be how you handle the sorting on the date column if it is treated as a string?

I solved that sorting problem by changing the property from type of string to a SmartDate.  In either case though, I'm only able to display the date, not the time.

Back to my original time not showing up in the DataGridView problem. What I don't understand is my BO's property is a type of SmartDate, and the SmartDate has its Date, DBValue property with both date and time value, only the text property of the SmartDate only contains the date part, why the time just doesn't show in the DataGridView for the column that is binded to my BO's SmartDate property? 

I hope I stated the problem clear.

 

 

 

 

RockfordLhotka replied on Friday, November 30, 2007

The Text property of a SmartDate is formatted according to the FormatString property value. The default format string is “d”, which is date-only. As I said in my previous post, you need to change the format string in the SmartDate to a different format to get the time too.

 

If you need to sort on the column, you may choose to expose both the SmartDate and a string property. Display the string property, and sort on the SmartDate property.

 

Rocky

yimiqi replied on Friday, November 30, 2007

Got it, even though I'm a bit slow...

Rocky, thank you so much for your helps!

Copyright (c) Marimer LLC