from DataGridView saving to text file issue when saving date values on vb.net

user3678904

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
SSS

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hash is not saving date as values?

From Dev

Saving list from python to text file in format

From Dev

Saving Date string to SQL Server date type column in asp.net (vb.net)

From Dev

VB.net Find And Replace from Data in a DataGridView in a text file

From Dev

issue with saving doc file

From Dev

Python : saving variables from a text file created

From Dev

Saving PairedRDD as a text file

From Dev

Saving text from TextView

From Dev

VB Getting The SubFolder name and saving it to a Text file

From Dev

Saving a DataGridView to a XML file

From Dev

Saving text file from code

From Dev

sorting collection of values from a text file and saving sorted values back to text file with pyspark

From Dev

Saving ArrayList to Text File

From Dev

Saving Date&Time from VB.Net to mysql Database Date?

From Dev

Saving a text file from flash

From Dev

Saving values from recurring function into file

From Dev

Saving XML file in VB.net aspx web app to a folder

From Dev

Django: When saving issue

From Dev

Saving a text from UIAlertView

From Dev

Saving date from datepicker

From Dev

saving multilined text from a textbox in vb.net

From Dev

Saving text from TextView

From Dev

ASP.NET MVC date not saving when posting

From Dev

VB Getting The SubFolder name and saving it to a Text file

From Dev

Permission denied when saving to file as text, a

From Dev

Saving ArrayList to Text File

From Dev

Minor Issue when saving vs retrieving file extension in java

From Dev

Saving file with date and user django

From Dev

Changes in Datagridview not saving in table SQLite vb.net