【问题标题】:TimeSpan DataFormatString in GridviewGridview 中的 TimeSpan DataFormatString
【发布时间】:2011-01-16 03:52:18
【问题描述】:

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

源变量之一是时间跨度,我将绑定字段与 DataField="MyTimeSpanVariable"。数据包含以秒和分钟为单位的时间,很少以小时为单位。

数据以其本机格式显示良好,但是当我将 HtmlEncode="false" DataFormatString="{0:hh:mm:ss}" 添加到 aspx 页面中的 boundfield 属性时,它在 MyGridView.Databind( ) 线。我做错了什么?

谢谢。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    http://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx 在 Asp.Net MVC 元数据中使用的方程式:

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

    【讨论】:

      【解决方案2】:

      我也有同样的问题!:
      FormatException on DataFormatString="Mødetidspunkt er {0:H:mm}"

      你不能使用两个冒号,所以使用短时间格式说明符。

      DataFormatString="Time is {0:t}"
      
      t 相当于 HH:mm T 相当于 HH:mm:ss

      "Standard DateTime Format Strings"

      【讨论】:

        【解决方案3】:

        从 aspx/ascx 这将类似于

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

        【讨论】:

          【解决方案4】:

          如果您使用 C#,我相信您需要转义第二个 ':'。例如:

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

          【讨论】:

            【解决方案5】:

            如果您愿意,可以尝试在 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
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2023-03-19
              • 2012-02-18
              • 2013-08-26
              • 2017-09-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多