Do not display datetime min value in gridview

Given:

public class Customer { public int Id { get; set; } public string FirstName { get; set; } public DateTime Birthdate { get; set; } } 

I have a list where some birth dates have not been entered, so when I bind to a GridView, it shows DateTime.MinValue. What are the different ways to show a space in gridview if the date of birth is DateTime.MinValue?

+1
c #
source share
3 answers

If this is not mapped to a database, I would make it null:

 public class Customer { public int Id { get; set; } public string FirstName { get; set; } public DateTime? Birthdate { get; set; } } 

EDIT:

If it is matched, then I would just make it a value of zero:

 public DateTime? BirthdateDisplay { get { if (this.Birthdate == default(DateTime)) return null; else return this.Birthdate; } } 
+9
source share

You can add an additional property for the Client named BirthDateStr.

 public class Customer { public int Id { get; set; } public string FirstName { get; set; } public DateTime Birthdate { get; set; } public string BirthDateStr { get { if (Birthdate != DateTime.MinValue) return Birthdate.ToString(); else return ""; } } 

Obviously, you could do whatever formatting you would like to do on the BirthDateStr in the getter to fit your format.

+3
source share

Alternatively, you can update the GridView frontend to hide DateTime.MinValue as follows:

 <asp:TemplateField HeaderText="Last Request Date"> <ItemTemplate><%# (DateTime)Eval("LastRequestDate").Equals(DateTime.MinValue) ? "" : string.Format("{0:dd/MM/yyyy hh:mm:ss tt}", (DateTime)Eval("LastRequestDate")) %></ItemTemplate> 

+1
source share

All Articles