这是我的webgrid。我正在尝试添加一个显示计算值的“EndDate”列
var grid = new WebGrid(canPage: true, rowsPerPage: Model.PageSize, canSort: true, ajaxUpdateContainerId: "grid");
grid.Bind(Model.MyRecords, rowCount: Model.TotalRecords, autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);
@grid.GetHtml(htmlAttributes: new { id = "grid" },
columns: grid.Columns(
grid.Column("StartDate", "Live Date"),
grid.Column("EndOffsetSeconds", "End in Seconds"),
grid.Column("StartDate"+TimeSpan.FromSeconds("OffsetSeconds"), "Difference"),
grid.Column(header: "Action", format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ID })),
grid.Column(format: (item) => Html.ActionLink("Copy", "CopyRecord", new { id = item.ID }))
));
我想用EndDate代替第三列。
Enddate = StartDate+TimeSpan.FromSeconds(EndOffsetSeconds)
在第3列,我想显示StartDate + TimeSpan.FromSeconds(EndOffsetSeconds)的结果
其中StartDate是DateTime类型,EndOffsetSeconds是Integer类型
示例DateTime StartDate = 05/20/2012 12:15:03 int EndOffsetSeconds = 124000;
如何使用Webgrid添加计算列?
提前谢谢
答案 0 :(得分:2)
您可以向模型添加属性并在get方法中进行计算。
class MyRecord
{
public DateTime StartDate {get;set;}
public int EndOffsetSeconds {get;set;}
public DateTime CalculatedEndTime
{
get
{
return StartDate + TimeSpan.FromSeconds( EndOffsetSeconds );
}
}
}
然后只需指向网格中的计算属性。