gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:11:26 25 4
gpt4 key购买 nike

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

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

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

这些是数据网格的代码:

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

最佳答案

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

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

关于mysql - 在 vb.net 上保存日期值时从 DataGridView 保存到文本文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24006284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com