Gridview中的TimeSpan DataFormatString

fre*_*hie 5 asp.net

我有一个带有来自linq查询的ObjectDataSource的gridview.

源的一个变量是时间跨度,我用DataField ="MyTimeSpanVariable"绑定一个boundfield.数据包含以秒和分钟为单位的时间,极少数以小时为单位.

数据以其原生格式显示正常,但是当我将HtmlEncode ="false"DataFormatString ="{0:hh:mm:ss}"添加到aspx页面中的boundfield属性时,它会在MyGridView.Databind()行上崩溃.我究竟做错了什么?

谢谢.

小智 5

请参阅http://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx Eq 以在 Asp.Net MVC 元数据中使用:

DisplayFormat(DataFormatString = "{0:hh\\:mm\\:ss}")
Run Code Online (Sandbox Code Playgroud)


小智 -1

如果您愿意,可以尝试在 Grid 的 RowDataBound 事件中设置字段格式。

例如:

Protected Sub YourGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles YourGridView.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.DataItem("YourColName") Then
            'Do formatting
        End If
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)