P
pb1990
Guest
Hi,
I get an issue everytime I export a Datagridview in a .csv file. Date time column, that look like this (dd/MM/yyyy) comes in the csv file like this (dd/MM/yyyy 00.00.00). It's something that I don't want because I already have a time column in that datagridview. Here is the code I'm using, how can I get rid of those 'zeros'?
SaveFileDialog5.Filter = "CSV Files (*.csv*)|*.csv"
SaveFileDialog5.Title = "Save CSV Files"
SaveFileDialog5.FileName = ("Backup Database GDI1 al " & DateAdd(DateInterval.Year, -2, Now()).ToString("dd_MM_yyyy"))
If SaveFileDialog5.ShowDialog() <> DialogResult.OK Then
Exit Sub
bd.Rows.Clear()
bd.Columns.Clear()
RichTextBox1.AppendText(vbCrLf & "" & DateTime.Now.ToString("dd/MM/yyyy HH':'mm':'ss") & " La compilazione del backup è stata cancellata.")
RichTextBox1.ScrollToCaret()
Else
End If
Dim fileName As String = Me.SaveFileDialog5.FileName
Try
Dim StrExport As String = ""
For Each C As DataGridViewColumn In BackupDataGridViews.Columns
StrExport &= """" & C.HeaderText & ""","
Next
StrExport = StrExport.Substring(0, StrExport.Length - 1)
StrExport &= Environment.NewLine
For Each R As DataGridViewRow In BackupDataGridViews.Rows
For Each C As DataGridViewCell In R.Cells
Dim timecolumnindex As Integer = Nothing
If C.ColumnIndex = timecolumnindex Then
If Not C.Value Is Nothing Then
StrExport &= """" & C.Value.ToString & ""","
Else
StrExport &= """" & "" & ""","
End If
Else
If Not C.Value Is Nothing Then
StrExport &= """" & C.Value.ToString & ""","
Else
StrExport &= """" & "" & ""","
End If
End If
Next
StrExport = StrExport.Substring(0, StrExport.Length - 1)
StrExport &= Environment.NewLine
Next
Dim tw As IO.TextWriter = New IO.StreamWriter(SaveFileDialog5.FileName)
tw.Write(StrExport)
tw.Close()
RichTextBox1.AppendText(vbCrLf & "" & DateTime.Now.ToString("dd/MM/yyyy HH':'mm':'ss") & " La creazione del file .CSV del backup è stata effettuata correttamente.")
RichTextBox1.ScrollToCaret()
Catch ex As System.Exception
RichTextBox1.AppendText(vbCrLf & "" & DateTime.Now.ToString("dd/MM/yyyy HH':'mm':'ss") & " " & ex.Message & ". La creazione del file .CSV del backup non è andata a buon fine.")
RichTextBox1.ScrollToCaret()
go = False
End Try
Thank you.
Continue reading...
I get an issue everytime I export a Datagridview in a .csv file. Date time column, that look like this (dd/MM/yyyy) comes in the csv file like this (dd/MM/yyyy 00.00.00). It's something that I don't want because I already have a time column in that datagridview. Here is the code I'm using, how can I get rid of those 'zeros'?
SaveFileDialog5.Filter = "CSV Files (*.csv*)|*.csv"
SaveFileDialog5.Title = "Save CSV Files"
SaveFileDialog5.FileName = ("Backup Database GDI1 al " & DateAdd(DateInterval.Year, -2, Now()).ToString("dd_MM_yyyy"))
If SaveFileDialog5.ShowDialog() <> DialogResult.OK Then
Exit Sub
bd.Rows.Clear()
bd.Columns.Clear()
RichTextBox1.AppendText(vbCrLf & "" & DateTime.Now.ToString("dd/MM/yyyy HH':'mm':'ss") & " La compilazione del backup è stata cancellata.")
RichTextBox1.ScrollToCaret()
Else
End If
Dim fileName As String = Me.SaveFileDialog5.FileName
Try
Dim StrExport As String = ""
For Each C As DataGridViewColumn In BackupDataGridViews.Columns
StrExport &= """" & C.HeaderText & ""","
Next
StrExport = StrExport.Substring(0, StrExport.Length - 1)
StrExport &= Environment.NewLine
For Each R As DataGridViewRow In BackupDataGridViews.Rows
For Each C As DataGridViewCell In R.Cells
Dim timecolumnindex As Integer = Nothing
If C.ColumnIndex = timecolumnindex Then
If Not C.Value Is Nothing Then
StrExport &= """" & C.Value.ToString & ""","
Else
StrExport &= """" & "" & ""","
End If
Else
If Not C.Value Is Nothing Then
StrExport &= """" & C.Value.ToString & ""","
Else
StrExport &= """" & "" & ""","
End If
End If
Next
StrExport = StrExport.Substring(0, StrExport.Length - 1)
StrExport &= Environment.NewLine
Next
Dim tw As IO.TextWriter = New IO.StreamWriter(SaveFileDialog5.FileName)
tw.Write(StrExport)
tw.Close()
RichTextBox1.AppendText(vbCrLf & "" & DateTime.Now.ToString("dd/MM/yyyy HH':'mm':'ss") & " La creazione del file .CSV del backup è stata effettuata correttamente.")
RichTextBox1.ScrollToCaret()
Catch ex As System.Exception
RichTextBox1.AppendText(vbCrLf & "" & DateTime.Now.ToString("dd/MM/yyyy HH':'mm':'ss") & " " & ex.Message & ". La creazione del file .CSV del backup non è andata a buon fine.")
RichTextBox1.ScrollToCaret()
go = False
End Try
Thank you.
Continue reading...