DataGridView中的日期和时间格式问题

时间:2015-09-28 14:02:49

标签: vb.net datagridview ms-access-2010

我的表单上有MonthCalendar控件。当您选择日期时,它会使用所选日期填充文本框。它在文本框中显示为我想要的格式(短日期)。

当我从我的文本框中将新记录添加到数据库时,它将在我的DataGridView控件中加载为(短日期和时间)。我打开Access文件,记录显示短日期的正确格式。 Access中的数据类型选择为日期/时间,格式为短日期。

来自哪里的时间以及如何摆脱它?

Public Class Form1
    Dim i As Integer
    Dim con As New OleDb.OleDbConnection        'THE CONNECTION OBJECT
    Dim dbProvider As String                    'HOLDS THE PROVIDER
    Dim dbSource As String                      'HOLDS THE DATA SOURCE
    Dim MyFolder As String                      'HOLDS THE DATABASE FOLDER
    Dim TheDatabase As String                   'HOLDS THE DATABASE NAME
    Dim FullDatabasePath As String              'HOLDS THE DATABASE PATH
    Dim ds As New DataSet                       'HOLDS THE DataSet OBJECT
    Dim da As OleDb.OleDbDataAdapter            'HOLDS THE DataAdapter OBJECT
    Dim sql As String                           'HOLDS THE SQL STRING
    Dim a As Control
    Dim str_pipedate As String


    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.PV_TableTableAdapter.Fill(Me.PVdbDataSet.PV_Table)

        'SET UP THE PROVIDER
        dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"

        'SET THE DATABASE AND WHERE THE DATABASE IS
        TheDatabase = "/PVdb.accdb"
        MyFolder = "C:\Pipe Vault"
        FullDatabasePath = MyFolder & TheDatabase

        'SET THE DATA SOURCE
        dbSource = "Data Source = " & FullDatabasePath

        'SET THE CONNECTION STRING
        con.ConnectionString = dbProvider & dbSource

        'OPEN THE DATABASE
        con.Open()

        'STORE THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
        sql = "SELECT * FROM PV_table"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "PV_Table")


        'Close THE DATABASE  '
        con.Close()
        'MessageBox.Show("Database is now closed")
    End Sub

    Private Sub DataGridView1_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
        'LOADS TEXTBOXS WITH DATA FROM DATAGRID OBJECT
        i = DataGridView1.CurrentRow.Index
        txtLocation.Text = DataGridView1.Item(0, i).Value
        txtProjectName.Text = DataGridView1.Item(1, i).Value
        txtPipeDate.Text = DataGridView1.Item(2, i).Value
        txtPipeNumber.Text = DataGridView1.Item(3, i).Value
        txtPipeSize.Text = DataGridView1.Item(4, i).Value
        txtAproxFootage.Text = DataGridView1.Item(5, i).Value
        txtInstallDate.Text = DataGridView1.Item(6, i).Value
    End Sub

    Private Sub TxtPipeDate_Click(sender As Object, e As EventArgs) Handles txtPipeDate.Click
        'MonthCalendar1.Visible = True
    End Sub

    Private Sub MonthCalendar1_DateSelected(sender As Object, e As DateRangeEventArgs) Handles MonthCalendar1.DateSelected
        txtPipeDate.Text = MonthCalendar1.SelectionStart
        MonthCalendar1.Visible = False
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

        Dim cb As New OleDb.OleDbCommandBuilder(da)
        ds.Tables("PV_table").Rows(i).Item(1) = txtLocation.Text
        ds.Tables("PV_table").Rows(i).Item(2) = txtProjectName.Text
        ds.Tables("PV_table").Rows(i).Item(3) = txtPipeDate.Text
        ds.Tables("PV_table").Rows(i).Item(4) = txtPipeNumber.Text
        ds.Tables("PV_table").Rows(i).Item(5) = txtPipeSize.Text
        ds.Tables("PV_table").Rows(i).Item(6) = txtAproxFootage.Text
        ds.Tables("PV_table").Rows(i).Item(7) = txtInstallDate.Text
        da.Update(ds, "PV_table")

        'MessageBox.Show("Data updated")
    End Sub

    Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click

        'ENABLES TEXT BOXES
        For Each a In Me.Controls
            If TypeOf a Is TextBox Then
                a.Enabled = True
            End If
        Next

        'CLEARS CONTENTS OF TEXT BOXES
        txtLocation.Clear()
        txtProjectName.Clear()
        txtPipeDate.Clear()
        txtPipeNumber.Clear()
        txtPipeSize.Clear()
        txtAproxFootage.Clear()
        txtInstallDate.Clear()

        'SETS STATES OF BUTTONS
        btnCommit.Enabled = True
        btnAddNew.Enabled = False
        btnUpdate.Enabled = False
        btnDelete.Enabled = False
    End Sub

    'Private Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
    '   btnCommit.Enabled = False
    '  btnAddNew.Enabled = True
    ' btnUpdate.Enabled = True
    'btnDelete.Enabled = True
    'End Sub

    Private Sub btnCommit_Click(sender As Object, e As EventArgs) Handles btnCommit.Click


        Dim newRow As DataRow = ds.Tables("PV_Table").NewRow()
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        cb.QuotePrefix = "["
        cb.QuoteSuffix = "]"

        newRow("Location") = txtLocation.Text
        newRow("Project Name") = txtProjectName.Text
        newRow("Pipe Date") = txtPipeDate.Text
        newRow("Pipe Number") = txtPipeNumber.text
        newRow("Pipe Size") = txtPipeSize.Text
        newRow("Aprox Footage") = txtAproxFootage.Text
        newRow("Install Date") = txtInstallDate.Text

        ds.Tables("PV_Table").Rows.Add(newRow)
        da.Update(ds, "PV_Table")
        Me.PV_TableTableAdapter.Fill(Me.PVdbDataSet.PV_Table)

        'SET UP THE PROVIDER
        dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"

        'SET THE DATABASE AND WHERE THE DATABASE IS
        TheDatabase = "/PVdb.accdb"
        MyFolder = "C:\Pipe Vault"
        FullDatabasePath = MyFolder & TheDatabase

        'SET THE DATA SOURCE
        dbSource = "Data Source = " & FullDatabasePath

        'SET THE CONNECTION STRING
        con.ConnectionString = dbProvider & dbSource

        'OPEN THE DATABASE
        con.Open()

        'STORE THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
        sql = "SELECT * FROM PV_table"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "PV_Table")

        'Close THE DATABASE  '
        con.Close()

        'ENABLES TEXT BOXES
        For Each a In Me.Controls
            If TypeOf a Is TextBox Then
                a.Enabled = False
            End If
        Next

        'CLEARS CONTENTS OF TEXT BOXES
        txtLocation.Clear()
        txtProjectName.Clear()
        txtPipeDate.Clear()
        txtPipeNumber.Clear()
        txtPipeSize.Clear()
        txtAproxFootage.Clear()
        txtInstallDate.Clear()

        'SETS STATES OF BUTTONS
        btnCommit.Enabled = False
        btnAddNew.Enabled = True
        btnUpdate.Enabled = False
        btnDelete.Enabled = False


    End Sub


    Private Sub txtPipeDate_GotFocus(sender As Object, e As EventArgs) Handles txtPipeDate.GotFocus
        MonthCalendar1.Visible = True
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

  

来自[在DataGridView中]

的时间

Access中的日期/时间值始终具有日期和时间组件。 Access本身的默认格式化行为是在时间午夜时禁止时间组件,因此2015-09-28 00:00:00的日期/时间默认显示为{ Access中的{1}}。

  

我该如何摆脱它?

.NET控件不共享日期/时间值的默认格式化行为,因此您只需要将DataGridView的特定列的格式设置为仅日期格式,如2015-09-28 “短日期”。您可以在表单的设计视图中执行此操作...

CellStyle.png

...或者使用像这样的代码

d

有关其他格式代码,请参阅

Standard Date and Time Format Strings