locked
CSV to datagridview & datagridview to CSV.

    Question

  • As an old fart into VB 2008 express .net as a hobby, can anyone please help me.
    I need the code to save a datagridview contnts to a CSV or delim text file, and
    fill a datagridview from the same as above.
    This is for my personal study not for any other use.

    Thanks.
    Friday, July 03, 2009 10:40 PM

Answers

  • O;d Fart

    here is something to get you started. It reads a CSV file into a DataTable (which is then shown in the DGV).

    Assuming you have column headers, it reads the first line and uses those as the datatable Column Names. It also keeps track of the number of rows. Then later when it saves the datatable, it starts from the first new row. You can set it up to save the entire table again if you want.

    Then it reads each subsequent line a adds a row to the datatable

    there is also code to add data from textboxes to the datatable.

    Then it saves each new ro added to the datatable into the CSV


    Public Class Form1
        Dim dt As New DataTable
        Dim filename As String = "C:\Users\Joe\Desktop\Test Folder\mreed72 test.txt"
        Dim rowcount As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filename)
                MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
                MyReader.Delimiters = New String() {","}
                Dim firstrow As String()
                Dim currentRow As String()
    
                If Not MyReader.EndOfData Then
                    firstrow = MyReader.ReadFields()
                    For i As Integer = 0 To UBound(firstrow)
                        dt.Columns.Add(firstrow(i))
                    Next
                End If
    
                While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        dt.Rows.Add(currentRow)
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MsgBox(ex.Message & vbCrLf & "Skipping")
                    End Try
                End While
            End Using
    
            DataGridView1.DataSource = dt
            rowcount = dt.Rows.Count
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)
            For Each ctrl As Control In Controls
                If TypeOf ctrl Is TextBox Then
                    ctrl.Text = ""
                End If
            Next
            TextBox1.Focus()
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim mystring As String = ""
            Using sw As New System.IO.StreamWriter("C:\Users\Joe\Desktop\Test Folder\mreed72 test.txt", True)
    
                For i As Integer = rowcount To dt.Rows.Count - 1
                    mystring = ""
                    For j As Integer = 0 To dt.Columns.Count - 1
                        mystring = mystring & dt.Rows(i).Item(j).ToString & ","
                    Next
                    mystring = mystring.Remove(mystring.Length - 1, 1)
                    sw.WriteLine(mystring)
                Next
            End Using
        End Sub
    End Class
    • Marked as answer by O;d Fart Saturday, July 04, 2009 6:16 AM
    Saturday, July 04, 2009 5:20 AM

All replies

  • O;d Fart

    here is something to get you started. It reads a CSV file into a DataTable (which is then shown in the DGV).

    Assuming you have column headers, it reads the first line and uses those as the datatable Column Names. It also keeps track of the number of rows. Then later when it saves the datatable, it starts from the first new row. You can set it up to save the entire table again if you want.

    Then it reads each subsequent line a adds a row to the datatable

    there is also code to add data from textboxes to the datatable.

    Then it saves each new ro added to the datatable into the CSV


    Public Class Form1
        Dim dt As New DataTable
        Dim filename As String = "C:\Users\Joe\Desktop\Test Folder\mreed72 test.txt"
        Dim rowcount As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filename)
                MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
                MyReader.Delimiters = New String() {","}
                Dim firstrow As String()
                Dim currentRow As String()
    
                If Not MyReader.EndOfData Then
                    firstrow = MyReader.ReadFields()
                    For i As Integer = 0 To UBound(firstrow)
                        dt.Columns.Add(firstrow(i))
                    Next
                End If
    
                While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        dt.Rows.Add(currentRow)
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MsgBox(ex.Message & vbCrLf & "Skipping")
                    End Try
                End While
            End Using
    
            DataGridView1.DataSource = dt
            rowcount = dt.Rows.Count
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)
            For Each ctrl As Control In Controls
                If TypeOf ctrl Is TextBox Then
                    ctrl.Text = ""
                End If
            Next
            TextBox1.Focus()
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim mystring As String = ""
            Using sw As New System.IO.StreamWriter("C:\Users\Joe\Desktop\Test Folder\mreed72 test.txt", True)
    
                For i As Integer = rowcount To dt.Rows.Count - 1
                    mystring = ""
                    For j As Integer = 0 To dt.Columns.Count - 1
                        mystring = mystring & dt.Rows(i).Item(j).ToString & ","
                    Next
                    mystring = mystring.Remove(mystring.Length - 1, 1)
                    sw.WriteLine(mystring)
                Next
            End Using
        End Sub
    End Class
    • Marked as answer by O;d Fart Saturday, July 04, 2009 6:16 AM
    Saturday, July 04, 2009 5:20 AM
  • Thanks a lot Jwavila. works a treat, just what I needed.
    Saturday, July 04, 2009 6:19 AM
  • jwavilla, Thanks .. that works great! but can you tell me how to update the same csv file which is bound to the datagridview? I am not able to update the csv at all
    Tuesday, March 15, 2011 2:58 PM
  • The Button2_Click event writes the datatable back to disk, although I think there's an error in the code.

    For i As Integer = rowcount To dt.Rows.Count - 1

    should read

    For i As Integer = 0 To dt.Rows.Count -1

     

    Tuesday, March 15, 2011 3:33 PM