Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webgrid Format of datetime and setting style

Tags:

razor

webgrid

I'm struggline with syntax gremlins with the WebGrid. In my normal razor markup i format a date inside my foreach like so

<td>
        @String.Format("{0:MM/dd/yy hh:mm:ss}", item.complianceedatetime)
    </td>

and I set my column width like so

<th width="150px">
        Download Date/Time
    </th>

How would I do this with the Grid.Column syntax

grid.Column("complianceedatetime", "Download Date/Time", ?, ?)
like image 711
Bayrat Avatar asked Mar 31 '11 17:03

Bayrat


2 Answers

@grid.GetHtml(
    column: grid.Columns(
              grid.Column("Complianceedatetime", "Download Date / Time", 
format: @<text>@item.complianceedatetime.ToString("MM/dd/yy hh:mm:ss")</text>)
            )
)

I know this works because I have this exact code in my project:

grid.Column(
            "PublishDate",
            canSort: true,
            format: @<text>@item.PublishDate.ToString("MM/dd/yyyy")</text>
        ),
like image 159
wtjones Avatar answered Nov 02 '22 03:11

wtjones


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)
like image 40
Mayank Avatar answered Nov 02 '22 02:11

Mayank