如何通过代码在详细信息视图网格中设置日期字段的格式?
我只是对使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在aspx页面找到详情视图,需要在绑定字段中添加
DataFromatString
标签。 IT 将会是这样的: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: