-
Oct 1st, 2024, 01:39 PM
#1
Thread Starter
Junior Member
Export textboxes with datagridview to .csv
Hello,
I am exporting data from DGV2 to a .csv file.
But now I need to export the text from 6 textboxes with
into this .csv file.
When importing, the data in DGV2 and in
the 6 text boxes again, is that possible?
Here is the code for save and import without the text boxes.
Save:
Code:
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
SaveFileDialog1.FileName = "Angebot vom" & " " & DateTimePicker1.Text & " " & KD_Anrede.Text & " " & KD_Name.Text & " " & KD_Vorname.Text
SaveFileDialog1.Filter = "CSV Files (*.csv*)|*.csv"
If SaveFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
Dim csvFile As String = String.Empty
csvFile = csvFile.TrimEnd(";")
csvFile = csvFile & vbCr & vbCrLf
'Used to ge the rows
For Each row As DataGridViewRow In Datagridview2.Rows
'Used to get each cell in the row
For Each cell As DataGridViewCell In row.Cells
csvFile = csvFile & cell.FormattedValue & ";"
Next
csvFile = csvFile.TrimEnd(";")
csvFile = csvFile & vbCr & vbCrLf
IO.File.WriteAllText(SaveFileDialog1.FileName, csvFile)
Next
End If
End Sub
Import :
Code:
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
OpenFileDialog1.Filter = "CSV Files (*.csv*)|*.csv"
OpenFileDialog1.FileName = "*.*"
If OpenFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
Datagridview2.Rows.Clear()
Dim TextFieldParser1 As New Microsoft.VisualBasic.FileIO.TextFieldParser(OpenFileDialog1.FileName)
TextFieldParser1.Delimiters = New String() {";"}
While Not TextFieldParser1.EndOfData
Dim Row1 As String() = TextFieldParser1.ReadFields()
If Datagridview2.Columns.Count = 0 AndAlso Row1.Count > 0 Then
Dim i As Integer
For i = 0 To Row1.Count - 1
Datagridview2.Columns.Add("Column" & i + 1, "Column" & i + 1)
Next
End If
Datagridview2.Rows.Add(Row1)
End While
End If
End Sub
Greetings
Andi
-
Oct 1st, 2024, 04:10 PM
#2
Re: Export textboxes with datagridview to .csv
Your code seems to be writing the entire collections for each DGV row. You only need to write the file once at the end.
Code:
Private Sub export_Click(sender As Object, e As EventArgs) Handles export.Click
Dim contents As String = ""
For r As Integer = 0 To DataGridView1.Rows.Count - 1
For c As Integer = 0 To DataGridView1.ColumnCount - 1
If c < DataGridView1.ColumnCount - 1 Then
contents &= If(DataGridView1.Rows(r).Cells(c).Value IsNot Nothing, DataGridView1.Rows(r).Cells(c).Value.ToString, "") & ";"
Else
contents &= If(DataGridView1.Rows(r).Cells(c).Value IsNot Nothing, DataGridView1.Rows(r).Cells(c).Value.ToString, "") & vbCrLf
End If
Next
Next
contents &= Me.TextBox1.Text & ";" & Me.TextBox2.Text
IO.File.WriteAllText("C:\AJunk2022\2022Test\NewCSV.csv", contents)
End Sub
-
Oct 2nd, 2024, 11:37 AM
#3
Thread Starter
Junior Member
Re: Export textboxes with datagridview to .csv
Hi wes4dbt,
when exporting, the data from the 6 text boxes are now also written to the .csv file, but how do I get the data when importing?
from the 6 textboxes into the textboxes again ? Is this possible within your code or do I have to use a separate code?
Greetings
Andi
-
Re: Export textboxes with datagridview to .csv
Originally Posted by andi-aston-martin
Hi wes4dbt,
when exporting, the data from the 6 text boxes are now also written to the .csv file, but how do I get the data when importing?
from the 6 textboxes into the textboxes again ? Is this possible within your code or do I have to use a separate code?
Greetings
Andi
Here's an example using a StreamReader,
Code:
Private Sub ImportButton_Click(sender As Object, e As EventArgs) Handles ImportButton.Click
Dim fName As String = ""
OpenFileDialog1.InitialDirectory = "c:\ajunk2022\2022Test"
OpenFileDialog1.Filter = "CSV files(*.csv)|*.csv"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
fName = OpenFileDialog1.FileName
End If
Dim TextLine As String = ""
Dim SplitLine() As String
If System.IO.File.Exists(fName) = True Then
Dim objReader As New System.IO.StreamReader(fName)
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
SplitLine = Split(TextLine, ";")
If objReader.Peek() <> -1 Then
Me.DataGridView1.Rows.Add(SplitLine)
Else
Me.TextBox1.Text = SplitLine(0)
Me.TextBox2.Text = SplitLine(1)
End If
Loop
objReader.Dispose()
Else
MsgBox("File Does Not Exist")
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|