5

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", ?, ?)
Bayrat
  • 161
  • 1
  • 3
  • 17

4 Answers4

8
@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>
        ),
wtjones
  • 4,090
  • 4
  • 34
  • 41
  • Changing `@item.complianceedatetime` to `@item.Complianceedatetime` would make the top snippet work properly. Otherwise, the second code snippet works. – Tim Hobbs Dec 10 '11 at 01:43
4

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)
Marko
  • 20,385
  • 13
  • 48
  • 64
Mayank
  • 334
  • 2
  • 11
1

You could try this:

@grid.GetHtml(
    column: grid.Columns(
              grid.Column("Complianceedatetime", "Download Date / Time", format: (item) => string.Format("{0:MM/dd/yy hh:mm:ss}", item.complianceedatetime)
            )
)
Nandu
  • 38
  • 4
0

Try this option for better globalization

    @grid.GetHtml(
        column: grid.Columns(
                  grid.Column("Complianceedatetime", "Download Date / Time", 
                  format: @@String.Format("{0:g}",complianceedatetime))
                )
    )
Hemant
  • 411
  • 4
  • 15
  • 1
    This doesn't work either. People, check your code please. It takes a minute to run it. It would really help for those of us trying to understand and learn the syntax if you would make sure you are posting correct code. – Chris Holmes Jul 19 '11 at 04:32