17

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:"PublishDate",rowsPerPage:10);
    %>
    <%: 
       grid.GetHtml(
                          tableStyle: "wGrid",
                            headerStyle: "wGridHeader",
                    alternatingRowStyle: "alt",
                    columns: grid.Columns(
                    grid.Column("Title", canSort: false),
                    grid.Column("PublishDate", "Published on"),
                    grid.Column("CategoryName", "Category"),
                    grid.Column(format: (item) => Html.ActionLink("Details", "Browse", new { id = item.Title }))
                  )
               )
    %>

Now I want to format the 'PublishDate' column to something like 'dd-MMM-yyyy'. Any idea how to do this?

tereško
  • 58,060
  • 25
  • 98
  • 150
Reza.Hoque
  • 2,690
  • 10
  • 49
  • 78

4 Answers4

41
grid.Column(
    "PublishDate", 
    "Published on",
    format: (item) => string.Format("{0:dd-MMM-yyyy}", item.PublishDate)
)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
12

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)
Mayank
  • 334
  • 2
  • 11
5

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"))),
Avery West
  • 51
  • 1
  • 1
0

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")))
ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110
Sanjana V
  • 125
  • 1
  • 3
  • 11