在vb.net上保存日期值时,从DataGridView保存到文本文件问题

时间:2014-06-03 02:39:30

标签: mysql vb.net

我有问题。每当我将数据从DataGridView保存到文本文件中时,日期的格式会随时间自动转换为日期。它不应该是这样的,因为datagridview所基于的数据只是纯粹的日期,但是当我将它传输到文本文件时,它会自动添加时间。它看起来像这样:

246,4/20/2013 12:00:00 AM,01:00:00,p

如何删除日期附带的时间(上午12:00:00)?

这些是datagrid的代码:

Private Sub load_table()

    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDA As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource


    Try
        sConnection.Open()
        Dim Query As String
        Query = "select * from swipe_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDA.SelectCommand = sqlCommand
        SDA.Fill(dbDataSet)
        bSource.DataSource = dbDataSet
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataSet)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub



Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDA As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource
    Try
        sConnection.Open()
        Dim Query As String
        Query = "select * from swipe_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDA.SelectCommand = sqlCommand
        SDA.Fill(dbDataSet)
        bSource.DataSource = dbDataSet
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataSet)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub

这些代码用于保存到文本文件部分:

Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    Dim filename As String = String.Empty
    Dim sfd1 As New SaveFileDialog()

    sfd1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    sfd1.FilterIndex = 2
    sfd1.RestoreDirectory = True
    sfd1.Title = "Save Text File"

    If sfd1.ShowDialog() = DialogResult.OK Then
        If sfd1.FileName = String.Empty Then
            MsgBox("Please input filename")
        Else
            filename = sfd1.FileName.ToString
            Saveto_TextFile(DataGridView1, filename)
        End If
    End If
End Sub

Sub Saveto_TextFile(ByVal dvList As DataGridView, ByVal filename As String)
    Dim strDestinationFile As String = "" & filename & ".txt"
    Dim tw As TextWriter = New StreamWriter(strDestinationFile)
    Dim LineToWrite As String = String.Empty

    Try
        For _Row As Integer = 0 To DataGridView1.Rows.Count - 1
            LineToWrite = String.Empty
            For _Column As Integer = 0 To DataGridView1.Columns.Count - 1
                If DataGridView1.Rows(_Row).Cells(_Column).Value IsNot Nothing Then

                    LineToWrite &= "," & DataGridView1.Rows(_Row).Cells(_Column).Value.ToString
                Else
                    LineToWrite &= ","
                End If
            Next
            LineToWrite = LineToWrite.Remove(0, 1) 'remove the first comma
            tw.WriteLine(LineToWrite)
        Next
        tw.Flush()
        tw.Close()
        MessageBox.Show("Data Saved Successfully")
    Catch ex As Exception
        MessageBox.Show("An error occurred")
    End Try
End Sub

2 个答案:

答案 0 :(得分:0)

没有"纯粹的约会"。 .NET DateTime类型始终表示日期和时间。我们接受的是,您可以将时间归零以表示日期,但时间部分直到那里,当您使用没有参数的ToString输出值时,您将看到它。您应该致电ToShortDateString或致电ToString并传递适当的格式说明符,例如" d"对于短系统日期格式或" M / dd / yyyy"美国日期格式。

答案 1 :(得分:0)

检查单元格值的类型......

For _Column As Integer = 0 To DataGridView1.Columns.Count - 1
  If DataGridView1.Rows(_Row).Cells(_Column).Value IsNot Nothing Then
    If DataGridView1.Rows(_Row).Cells(_Column).Value.GetType Is GetType(Date) AndAlso CDate(DataGridView1.Rows(_Row).Cells(_Column).Value).TimeOfDay = New Date().TimeOfDay Then
      LineToWrite &= "," & CDate(DataGridView1.Rows(_Row).Cells(_Column).Value).ToShortDateString
    Else
      LineToWrite &= "," & DataGridView1.Rows(_Row).Cells(_Column).Value.ToString
    End If
  Else
    LineToWrite &= ","
  End If
Next