supriya nikam

supriya nikam

  • 1.6k
  • 86
  • 4.3k

trying to write to a csvfile from a data set

Feb 13 2021 11:31 AM
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Build the CSV file data as a Comma separated string.
        Dim csv As String = String.Empty

        'Add the Header row for CSV file.
        For Each column As DataGridViewColumn In dataGridView1.Columns
            csv += column.HeaderText & ","c
        Next

        'Add new line.
        csv += vbCr & vbLf

        'Adding the Rows
        For Each row As DataGridViewRow In dataGridView1.Rows
            For Each cell As DataGridViewCell In row.Cells
                'Add the Data rows.
                csv += cell.Value.ToString().Replace(",", ";") & ","c
            Next

            'Add new line.
            csv += vbCr & vbLf
        Next

        'Exporting to Excel
        Dim folderPath As String = "C:\CSV\"
        File.WriteAllText(folderPath & "DataGridViewExport.csv", csv)
    End Sub

Answers (2)