Results 1 to 7 of 7

Thread: Saving DataGridView to csv? | SOLVED

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Saving DataGridView to csv? | SOLVED

    Hello! I am following a tutorial on how to save a DataGridView to csv and it won't save. I followed the tutorial correctly, something I did wrong?
    Code:
    Public Class frmMain
        Private Sub btnSubmitData_Click(sender As Object, e As EventArgs) Handles btnSubmitData.Click
            DataGridView1.Rows.Add(txtName.Text, txtCredit.Text, txtPrize.Text, txtPurchaser.Text, 3)
        End Sub
    
        Private Sub btnSaveData_Click(sender As Object, e As EventArgs) Handles btnSaveData.Click
            'Create Empty String
            Dim thecsvfile As String = String.Empty
            'get column headers
            For Each column As DataGridViewColumn In DataGridView1.Columns
                thecsvfile = thecsvfile & column.HeaderText & ","
            Next
            'trim the last comma
            thecsvfile = thecsvfile.TrimEnd(",")
            'add the line to the output
            thecsvfile = thecsvfile & vbCr & vbLf
            'get the rows
            For Each row As DataGridViewRow In DataGridView1.Rows
                'get the cells
                For Each cell As DataGridViewCell In row.Cells
                    thecsvfile = thecsvfile & cell.FormattedValue & ","
                Next
                'trim the last comma
                thecsvfile = thecsvfile & vbCr & vbLf
            Next
            'write file
            My.Computer.FileSystem.WriteAllText("export.csv", thecsvfile, False)
        End Sub
    End Class
    Shouldn't the data save automatically to the debug? Plan to do a save dialog after main functionality works.

    SOLUTION:
    Code:
            SaveFileDialog1.Filter = "CSV File (*.csv*)|*.csv"
            SaveFileDialog1.ShowDialog()
            My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, thecsvfile, False)
    Last edited by Dragnorian; May 23rd, 2017 at 01:16 AM. Reason: Solution Found

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Saving DataGridView to csv?

    What does this:
    it won't save
    actually mean? If the code is getting to this line:
    vb.net Code:
    1. My.Computer.FileSystem.WriteAllText("export.csv", thecsvfile, False)
    then either it is going to save whatever text you have to the file specified or it's going to throw an exception. Which is it? If it's the former, have you confirmed before that line is executed that 'thecsvfile' contains what you expect it to? If it does then the text is being saved and, if you can't see it, you must be looking in the wrong place for it. Where exactly are you looking?

    If you provide a file name and no path then it is assumed to be the current directory. The current directory is generally, although not always, the folder that the current EXE was run from. It can change while an application is running though, depending on what the code does. It is good practice to ALWAYS provide a full file path. If what you want is a file in the folder containing the EXE then that is what you should specify. In a WinForms app, that could look like this:
    vb.net Code:
    1. Dim filePath = IO.Path.Combine(Application.StartupPath, fileName)
    or
    vb.net Code:
    1. Dim filePath = My.Computer.FileSystem.CombinePath(Application.StartupPath, fileName)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Saving DataGridView to csv?

    Alright, for some reason I thought the method originally would work. I got that down. Now I am working on loading. I got most of the functionality to work with one issue: When I load in, it is missing a row. I am still trying to understand the code itself so any chance you can help?

    Code:
        Private Sub btnLoadData_Click(sender As Object, e As EventArgs) Handles btnLoadData.Click
            Dim fName As String = ""
            OpenFileDialog1.InitialDirectory = "c:\"
            OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
            OpenFileDialog1.FilterIndex = 2
            OpenFileDialog1.RestoreDirectory = True
            OpenFileDialog1.ShowDialog()
            fName = OpenFileDialog1.FileName
            Dim TextLine As String = ""
            If IO.File.Exists(fName) Then
                Dim Contents = IO.File.ReadAllLines(fName).ToList
    
                Contents.RemoveAt(0)
                Contents.RemoveAt(Contents.Count - 1)
                Contents.RemoveAt(Contents.Count - 1)
    
                For x As Integer = 0 To Contents.Count - 1
                    Contents(x) = Contents(x).Replace(",,,", ",").Replace(",,", ",")
                    Contents(x) = Contents(x).Substring(0, Contents(x).Count - 1)
                    Dim RowItem = Contents(x).Split(","c)
                    DataGridView1.Rows.Add(RowItem)
                Next
            End If
        End Sub

    In the saved data file itself(viewed in excel for confirmation), I saved 5 rows of data(not including base columns itself) and it saved 5 rows of data but when I load it into the application, only 4 rows of data, not 5 which is what I find weird.
    Last edited by Dragnorian; May 22nd, 2017 at 05:31 PM. Reason: Added Information

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Saving DataGridView to csv?

    Quote Originally Posted by Dragnorian View Post
    I got that down. Now I am working on loading.
    As the title states, this thread is about saving data. If that issue is resolved and you now have a question about loading data, please use the Thread Tools menu to mark this thread as Resolved and create a new thread with a title that indicates the new topic. Also, it's generally considered good practice to provide the solution if you come by one yourself. It may help others with similar issues but it may also help you if we can see a better way to do it.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: Saving DataGridView to csv?

    Sorry, just figured you'd know something about the loading too. Okay, thanks too btw

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Saving DataGridView to csv?

    Quote Originally Posted by Dragnorian View Post
    Sorry, just figured you'd know something about the loading too.
    And if I do then I'll provide that in another thread dedicated to that topic. One thread per topic and one topic pre thread helps keep the forum organised and easier to use for everyone.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Saving DataGridView to csv?

    I ask again:
    Quote Originally Posted by jmcilhinney View Post
    please use the Thread Tools menu to mark this thread as Resolved

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