don't display datetime min value in gridview

前端 未结 3 1263
死守一世寂寞
死守一世寂寞 2021-01-21 16:19

given:

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

相关标签:
3条回答
  • 2021-01-21 16:21

    You can add an additional property to the Customer called 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'd like to do on the BirthDateStr in the getter to make it match your format.

    0 讨论(0)
  • 2021-01-21 16:25

    Alternatively, you can update your front-end GridView display to hide out the DateTime.MinValue as follow:

    <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>
    

    0 讨论(0)
  • 2021-01-21 16:29

    Unless that's mapped to a Database, I'd make it nullable:

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

    EDIT:

    If it IS mapped, then I'd just make a readonly nullable value for it:

    public DateTime? BirthdateDisplay
    {
       get
       {
          if (this.Birthdate == default(DateTime))
             return null;
          else
             return this.Birthdate;
       }   
    }
    
    0 讨论(0)
提交回复
热议问题