How to format date in mvc web grid

前端 未结 4 1451
滥情空心
滥情空心 2021-02-05 11:03

I have a web grid and I am not using razor syntex.Rather I am using the .aspx form. the code is below;

<%
    var grid = new WebGrid(Model,defaultSort:\"Publi         


        
相关标签:
4条回答
  • 2021-02-05 11:17

    This works for me, and allows for Null values

    grid.Column("End_Date",format: item => ((item.End_Date == null) ? "" : item.End_Date.ToString("MM/dd/yyyy"))),
    
    0 讨论(0)
  • 2021-02-05 11:19

    This worked for me:

    grid.Column("Date_Of_Birth", "Date Of Birth", format: item => ((item.Date_Of_Birth == null) ? "" : item.Date_Of_Birth.ToString("MM/dd/yyyy")))
    
    0 讨论(0)
  • 2021-02-05 11:24
    grid.Column(
        "PublishDate", 
        "Published on",
        format: (item) => string.Format("{0:dd-MMM-yyyy}", item.PublishDate)
    )
    
    0 讨论(0)
  • 2021-02-05 11:35

    If DateTime Property is defined as (can contain null) :

    public DateTime? WorkedDate { get; set; }
    

    Use this format:

    grid.Column("WorkedDate", "Last Worked On",
       format: (item) => item.WorkedDate != null 
       ? item.WorkedDate.ToString("MM/dd/yy") : "", canSort: true)
    

    Otherwise if it is defined as below (can't be null), it will have either actual date or .MinDate as the default.

    public DateTime WorkedDate { get; set; }
    

    Use format:

    grid.Column("WorkedDate", "Last Worked On",
       format: (item) => item.WorkedDate != DateTime.MinValue ? 
       item.WorkedDate.ToString("MM/dd/yy") : "", canSort: true)
    
    0 讨论(0)
提交回复
热议问题