Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview--how to not show invalid dates? 1

Status
Not open for further replies.

ChristianWhite

Programmer
Jun 14, 2004
41
0
0
US
First time programming a full three-tier application, and I've been handed a biggie (as projects go in my group).

When I retrieve a datetime into a c# middle-tier object datetime property from the data tier, the datetime has to have a value. Not always having a valid date, I initialize it to Datetime.MinValue.

As we just went through a week of training for c# beginners ending with a demo of how the UI can manipulate objects to create gridviews via the object data source, I thought I'd put the practice to use.

Problem is, my gridview is reading from the object's datetime property that has to have a value even if the value isn't valid for business purposes.

Can I blank out cells in a gridview that have a specified value, such as DateTime.Minvalue? No fair falling back on SQL--this has to happen in the UI or POSSIBLY an object property. But to make the property a different type, I'd have to see an advantage. I'd rather filter via GridView and other UI objects.
 
if the can be null but your using DateTime.MinValue, I would consider this code smell. Use DateTime? (which is eye candy for Nullable<DateTime>) instead of DateTime. Or place display logic in the GridViewRowDataBoundEvent.
Code:
//is data row, then
if(TheDateTimeProperty == default(DateTime))
{
   DateTimeLable.Text = string.Empty;
}
else
{
   DateTimeLable.Text = TheDateTimeProperty.ToString("[formatted]");
}
I would opt for DateTime? since the value can be null. And this adheres to the DRY principle. if you go with the GUI formatting you will need to repeat this logic all over the place.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top