I have a problem. Whenever I save data into a text file from a DataGridView, the format of the date gets automatically converted to date along with time. It shouldn't be like that because the data from where the datagridview is basing from is only pure date but when I transfer it into a text file it automatically adds a time. It looks something like this:
246,4/20/2013 12:00:00 AM,01:00:00,p
How do I remove the time (12:00:00 AM) that comes along with the date?
These are codes for the 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
And these codes are for the saving to text file part:
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
Check the type of the cell value...
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments