如何通过代码在详细信息视图网格中设置日期字段的格式?

发布于 2024-12-09 11:17:45 字数 1074 浏览 0 评论 0原文

我只是对使用 DataReader 获得的数据(包含日期值)进行数据绑定,如以下代码所示:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim id As Integer = Request.QueryString("id")

    'Start connection
    Dim cs As String = ConfigurationManager.ConnectionStrings("Access 2010").ConnectionString
    Dim cn As New OleDbConnection(cs)
    cn.Open()

    'Retrieve Jobs Data
    Dim cmd As New OleDbCommand
    With cmd
        .Connection = cn
        .CommandText = "SELECT Nome, Descricao, Data, Autor FROM Jobs WHERE ID = @ID"
        .CommandType = CommandType.Text
        .Parameters.AddWithValue("@ID", id)
    End With
    Dim dbReader As OleDbDataReader = cmd.ExecuteReader()

    If Not Page.IsPostBack Then
        'Populate Dropdown Jobs
        dtlJob.DataSource = dbReader
        dtlJob.DataBind()
    End If
    dbReader.Close()
End Sub

并且我得到的值类似于 30/12/1899 00:00: 51。我检查了我的数据库,记录是正确的:显示日期为 12/10/2011。由于我刚刚使用了 .DataSource.DataBind,如何设置日期字段的格式?

I'm simply databinding the data (containing date values) I got with a DataReader as in the following code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim id As Integer = Request.QueryString("id")

    'Start connection
    Dim cs As String = ConfigurationManager.ConnectionStrings("Access 2010").ConnectionString
    Dim cn As New OleDbConnection(cs)
    cn.Open()

    'Retrieve Jobs Data
    Dim cmd As New OleDbCommand
    With cmd
        .Connection = cn
        .CommandText = "SELECT Nome, Descricao, Data, Autor FROM Jobs WHERE ID = @ID"
        .CommandType = CommandType.Text
        .Parameters.AddWithValue("@ID", id)
    End With
    Dim dbReader As OleDbDataReader = cmd.ExecuteReader()

    If Not Page.IsPostBack Then
        'Populate Dropdown Jobs
        dtlJob.DataSource = dbReader
        dtlJob.DataBind()
    End If
    dbReader.Close()
End Sub

And I'm getting the values like 30/12/1899 00:00:51. I checked my database and the record is correct: it appears 12/10/2011. How do I format the Date field since I just used .DataSource and .DataBind?

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

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

发布评论

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

评论(1

白云不回头 2024-12-16 11:17:45

在aspx页面找到详情视图,需要在绑定字段中添加DataFromatString标签。 IT 将会是这样的:

<asp:BoundField DataField="datetimefield" HeaderText="DateTime" DataFormatString="{0:d}" />

Find the details view on the aspx page and you need to add the DataFromatString tag to the bound field. IT will be something like this:

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