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?
Shouldn't the data save automatically to the debug? Plan to do a save dialog after main functionality works.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
SOLUTION:
Code:SaveFileDialog1.Filter = "CSV File (*.csv*)|*.csv" SaveFileDialog1.ShowDialog() My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, thecsvfile, False)




Reply With Quote
