Date Type Custom Format Provider?

  • Thread starter Thread starter McNutt Consulting
  • Start date Start date
M

McNutt Consulting

All,

I have a class with a date field

<Serializable()> Public Class Account

Private _startdate As Date
Public Property StartDate() as Date
Get
return _startdate
End Get
Set(ByVal Value As Date)
_startdate = Value
End Set
End Property

....class properties/methods

End Class

When this is bound to a datagrid, it shows 1/1/0001. I understand why and
all the value type/reference type discussion I've read are clear. However,
when _startdate.equals(date.minvalue), I don't want to display anything in
the datagrid or anywhere else for that matter. I want StartDate.ToString to
return String.Empty.

Is this possible with a custom format provider or in any other way?

Thanks for you time,
Larry
 
Hi Larry,

One option would be that when you do the databinding, you can handle the
format event of the binding object and convert DateTime.MinValue to an
empty string and in the Parse event of the binding you can convert empty
string to DateTime.MinValue.

One other option would be to write a new DateTime that encapsulates the
framework DateTime and exposes the same interface as DateTime, execpt
that the ToXXXString() methods would first check if the value ==
MinValue, and if so would return an empty string. This seems quite
tedious. Ofcourse it could have been easier if you could inherit from
DateTime.

Implementing a IFormatProvider would not help as far as i see, because
the UI controls never make use of this interface.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
Back
Top