Results 1 to 3 of 3

Thread: Speichern (saving) / Öffnen (opening) einer CSV-Datei von und zu einer DataGridView

  1. #1

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Speichern (saving) / Öffnen (opening) einer CSV-Datei von und zu einer DataGridView

    I hope the mods don't mind me titling this post mostly in German. Basically the below code is for saving and opening CSV files from and to a DataGridView.

    Saving DataGridView data to a CSV file:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            SaveFileDialog1.Filter = "CSV Files (*.csv*)|*.csv"
            SaveFileDialog1.FileName = ""
    
            If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
                ' The DataGridView1.ClipboardCopyMode line ensures that the data stays in the correct column and row for saving without headers
    
                DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
    
                DataGridView1.SelectAll()
    
                Try
                    IO.File.WriteAllText(SaveFileDialog1.FileName, DataGridView1.GetClipboardContent().GetText(TextDataFormat.CommaSeparatedValue))
                Catch ex As Exception
                    MessageBox.Show("This file is open in another application. Either change the filename or close the application using the file you're trying to update.")
                End Try
    
                DataGridView1.ClearSelection()
            End If
    
        End Sub

    Finding and opening a CSV file to a DataGridView:
    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            OpenFileDialog1.Filter = "CSV Files (*.csv*)|*.csv"
            OpenFileDialog1.FileName = ""
    
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
                DataGridView1.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 DataGridView1.Columns.Count = 0 AndAlso Row1.Count > 0 Then
                        Dim i As Integer
    
                        For i = 0 To Row1.Count - 1
                            DataGridView1.Columns.Add("Column" & i + 1, "Column" & i + 1)
                        Next
                    End If
    
                    DataGridView1.Rows.Add(Row1)
                End While           
    
            End If
    
        End Sub
    JMC informed a member about using TextFieldParser for CSV files, which I Googled, finding the opening method above which was coded by David on Stack Overflow:
    https://stackoverflow.com/questions/...grid-in-vb-net

    I hope someone finds this useful.
    Last edited by Peter Porter; Nov 16th, 2020 at 06:14 PM.

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Speichern (saving) / Öffnen (opening) einer CSV-Datei von und zu einer DataGridVi

    Quote Originally Posted by Peter Porter View Post

    ChrisE informed a member about using TextFieldParser for CSV files, which I Googled and found the opening method above which was coded by David on Stack Overflow:
    https://stackoverflow.com/questions/...grid-in-vb-net

    I hope someone finds this useful.
    it was actually JMC in Post#4 that said ...If you do want to stick with a CSV format, use a TextFieldParser to read the data.

    using a Schema.ini is also an option for .CSV files
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Speichern (saving) / Öffnen (opening) einer CSV-Datei von und zu einer DataGridVi

    Quote Originally Posted by ChrisE View Post
    it was actually JMC in Post#4 that said ...If you do want to stick with a CSV format, use a TextFieldParser to read the data.

    using a Schema.ini is also an option for .CSV files
    D'oh! I corrected my post.

Tags for this Thread

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