C# 如何格式化自动生成的WebGrid日期列

C# 如何格式化自动生成的WebGrid日期列,c#,asp.net-mvc,datetime,formatting,webgrid,C#,Asp.net Mvc,Datetime,Formatting,Webgrid,我看到有很多答案,例如如何单独设置DateTimeWebGrid列的格式,但是是否可以将WebGrid设置为自动设置所有类型DateTime列的格式 我这样问是因为我的WebGrid自动从我的类中提取数据,所以我从来没有真正访问过WebGrid.Column,因为我正在(尝试)使用一个通用的WebGrid视图: @model IEnumerable<MVCQCPage.Helpers.IGriddable> @{ var grid = new WebGrid(new Lis

我看到有很多答案,例如如何单独设置
DateTime
WebGrid
列的格式,但是是否可以将
WebGrid
设置为自动设置所有类型
DateTime
列的格式

我这样问是因为我的
WebGrid
自动从我的类中提取数据,所以我从来没有真正访问过
WebGrid.Column
,因为我正在(尝试)使用一个通用的
WebGrid
视图:

@model IEnumerable<MVCQCPage.Helpers.IGriddable>

@{
    var grid = new WebGrid(new List<object>());
    var linkPrefix = string.Empty;
    var selectable = false;

    if (Model.Count() > 0)
    {
        string[] columnNames = { };

        var firstItem = Model.First();

        linkPrefix = firstItem.RowLinkPrefix;
        columnNames = firstItem.ColumnHeaders;
        selectable = firstItem.Selectable;

        grid = new WebGrid(Model,
            rowsPerPage: 100,
            columnNames: columnNames);
    }
}
<div id="grid">
    @grid.GetHtml(
            tableStyle: "table",
            alternatingRowStyle: "alternate",
            headerStyle: "header"
        )
</div>
因此,实现它的每个类可能包含如下内容:

public interface IGriddable
{
    string RowLinkPrefix { get; }
    string[] ColumnHeaders { get; }
    bool Selectable { get; }
}
public string[] ColumnHeaders { get; } = new string[] { "SampleNo", "QCDate", "StatusClerk", "StatusSupervisor" };
public string RowLinkPrefix { get { return $"/receiving/{Pallet.Grv.GRVNo}/{Pallet.PalletSeq}/"; } }
public bool Selectable { get; } = true;
我尝试将
Displayformat
添加到
QCDate
属性,但这没有帮助


非常感谢您的帮助或建议,谢谢

我通过修改我的属性来解决这个问题,如下所示:

public DateTime QCDate { get; set; }
public string Date { get { return QCDate.ToString("dd/MM/yyyy"); } } 
并将
列标题更改为

public string[] ColumnHeaders { get; } = new string[] { "SampleNo", "Date", "StatusClerk", "StatusSupervisor" };
因此,当该项显示在网格中时,它现在将显示我需要的短日期字符串