将 DetailsView TemplateField 单元格值转换为短日期字符串

发布于 2024-09-18 17:37:53 字数 707 浏览 4 评论 0原文

我有一个详细信息视图,其单元格中的日期值当前以 longDateFormat 显示,我想将此详细信息视图中的所有日期值转换为短日期。

例如,我想只显示 6/1/2010 12:00:00 AM,而不是 6/1/2010

对于 Gridview,我可以通过以下方式实现下面的代码

  Protected Sub DetailsView4_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView4.DataBound

    If e.Row.RowType = DataControlRowType.DataRow Then
        For i As Integer = 0 To e.Row.Cells.Count - 1
            Dim cellDate As Date
            If Date.TryParse(e.Row.Cells(i).Text, cellDate) Then
                e.Row.Cells(i).Text = String.Format("{0:d}", cellDate)
            End If
        Next
    End If

End Sub

如何用DetailsView 实现相同的效果?

I have a detailsView whose date values in a cell are currently being displayed in longDateFormat, i want to convert all date values in this DetailsView to short date.

For example, instead of 6/1/2010 12:00:00 AM, i want to display just 6/1/2010

For a Gridview, i can achieve that by the code blow

  Protected Sub DetailsView4_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView4.DataBound

    If e.Row.RowType = DataControlRowType.DataRow Then
        For i As Integer = 0 To e.Row.Cells.Count - 1
            Dim cellDate As Date
            If Date.TryParse(e.Row.Cells(i).Text, cellDate) Then
                e.Row.Cells(i).Text = String.Format("{0:d}", cellDate)
            End If
        Next
    End If

End Sub

How can achieve the same with a DetailsView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-09-25 17:37:53

它可以简单地实现,如果它在模板字段中..

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("tDate", "{0:MM/dd/yyyy}") %>'></asp:TextBox>

或者如果它不是模板字段那么

<asp:BoundField DataField="tDate" HeaderText="tDate" SortExpression="tDate" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" />

It can be achieved simply, if it is in template filed..

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("tDate", "{0:MM/dd/yyyy}") %>'></asp:TextBox>

or if it is not template field then

<asp:BoundField DataField="tDate" HeaderText="tDate" SortExpression="tDate" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文