开发者

TimeSpan DataFormatString in Gridview

I have a gr开发者_开发百科idview with an ObjectDataSource that comes from a linq query.

One of the variables of the source is a timespan and I'm binding a boundfield with the DataField="MyTimeSpanVariable". The data contains times in seconds and minutes and very rarely in hours.

The data displays fine in its native format but when I add HtmlEncode="false" DataFormatString="{0:hh:mm:ss}" to the boundfield properties in the aspx page, it crashes on the MyGridView.Databind() line. What am I doing wrong?

Thanks.


See http://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx Eq to use in Asp.Net MVC Metadata:

DisplayFormat(DataFormatString = "{0:hh\\:mm\\:ss}")


I have the same problem!:
FormatException on DataFormatString="Mødetidspunkt er {0:H:mm}"

You can't use two colons, so use the short time format specifier.

DataFormatString="Time is {0:t}"
t is the equivalent of HH:mm
T is the equivalent of HH:mm:ss

See "Standard DateTime Format Strings".


I believe you need to escape the second ':' if you are using C#. For example:

DataFormatString=@"Time is {0:H\:mm}";


From the aspx/ascx this would be something like

HtmlEncode="false" DataFormatString="{0:mm\:ss}" 


If you like, you can try formatting your field on the Grid's RowDataBound event.

For example:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜