Results 1 to 7 of 7

Thread: [RESOLVED] Export textboxes with datagridview to .csv

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2024
    Posts
    40

    Resolved [RESOLVED] 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

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,272

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2024
    Posts
    40

    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

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,272

    Re: Export textboxes with datagridview to .csv

    Quote Originally Posted by andi-aston-martin View Post
    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

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2024
    Posts
    40

    Re: Export textboxes with datagridview to .csv

    Hi wes4dbt,

    thanks it works great.

    Greetings
    Andi

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2024
    Posts
    40

    Re: Export textboxes with datagridview to .csv

    Hi wes4dbt,

    I have a problem with importing from DGV1 to DGV2.
    I have created a new form because of our inventory.
    From DGV1 I first export all rows to DGV2 and save them in .csv file, so far everything is great.
    In DGV2 I have added 2 columns because of our inventory
    “ INGMG “ and ‘ Difference ’.
    The problem is where I can't figure it out:
    1) If I export all rows from DGV1 to DGV2 without saving them first in .csv file and enter the new stock in DGV2 column “INGMG” and click on Update Stocks, the new stocks from DGV2 are transferred to DGV1 in column “INGMG”.

    2) If I export all rows from DGV1 to DGV2 and save them first, then import them back into DGV2 and only then enter the new stock in DGV2 column “INGMG”, the following error message appears:
    System.InvalidCastException: “Invalid conversion from string to decimal type.” .
    The error occurs in this line of code:

    Code:
    Me.Validate()
    Me.TEILEBindingSource.EndEdit()
    
    
    Dim row As TeileDataSet.TEILERow
    
    For i = 0 To Me.DataGridView2.Rows.Count - 1
        Dim dgvrow As DataGridViewRow = Me.DataGridView2.Rows(i)
        If (dgvrow IsNot Nothing) AndAlso (Not dgvrow.IsNewRow) Then
            'The PrimaryKey for this table is the LotId field
            row = TeileDataSet.TEILE.FindByID(dgvrow.Cells("IDTeileNR").Value.ToString)
            If row IsNot Nothing Then
     --->    row("INGMG") = CDec(dgvrow.Cells("INGMG").Value)
    
    
    
            End If
        End If
    
    Next
    Can't figure out what it is...

    Greetings Andi

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,272

    Re: [RESOLVED] Export textboxes with datagridview to .csv

    Don't really understand what your problem is.

    1) If I export all rows from DGV1 to DGV2 without saving them first in .csv file and enter the new stock in DGV2 column “INGMG” and click on Update Stocks, the new stocks from DGV2 are transferred to DGV1 in column “INGMG”.
    What did you expect to happen? Post the relevant code.

    2) If I export all rows from DGV1 to DGV2 and save them first, then import them back into DGV2 and only then enter the new stock in DGV2 column “INGMG”, the following error message appears:
    System.InvalidCastException: “Invalid conversion from string to decimal type.” .
    When the error occurrs check the value of "dgvrow.Cells("INGMG").Value" With the debugger. What is it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width