In this article I will explain with an example, how to export DataTable data to CSV file in Windows Forms (WinForms) Application using C# and VB.Net.
CSV file is nothing but a Text file where data (records) are stored separated (delimited) by comma character.
 
 
Form Controls
I have added a DataGridView and a Button to the Windows Form.
Export DataTable to CSV (Text) File in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
 
VB.Net
Imports System.IO
Imports System.Data
 
 
Populating DataGridView
The DataGridView is populated with a dynamic DataTable with some sample data.
C#
public Form1()
{
    InitializeComponent();
    this.BindDataGridView();
}
 
private void BindDataGridView()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
            new DataColumn("Name", typeof(string)),
            new DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    this.dataGridView1.DataSource = dt;
}
 
VB.Net
Public Sub New()
    InitializeComponent()
    Me.BindDataGridView()
End Sub
 
Private Sub BindDataGridView()
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)), _
                                           New DataColumn("Name", GetType(String)), _
                                           New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    Me.dataGridView1.DataSource = dt
End Sub
 
Export DataTable to CSV (Text) File in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Exporting DataGridView data to CSV file
When the Export button is clicked, the following event handler is executed. A loop is executed over the Columns and Rows of the DataGridView and a comma separated (delimited) string is generated.
Finally comma separated (delimited) string is written to the CSV (Text) file using the File class.
C#
private void btnExport_Click(object sender, EventArgs e)
{
    //Build the CSV file data as a Comma separated string.
    string csv = string.Empty;
 
    //Add the Header row for CSV file.
    foreach (DataGridViewColumn column in dataGridView1.Columns)
    {
        csv += column.HeaderText + ',';
    }
 
    //Add new line.
    csv += "\r\n";
 
    //Adding the Rows
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        foreach (DataGridViewCell cell in row.Cells)
        {
            //Add the Data rows.
            csv += cell.Value.ToString().Replace(",", ";") + ',';
        }
 
        //Add new line.
        csv += "\r\n";
    }
 
    //Exporting to CSV.
    string folderPath = "C:\\CSV\\";
    File.WriteAllText(folderPath + "DataGridViewExport.csv", csv);
}
 
VB.Net
Private Sub btnExport_Click(sender As System.Object, e As System.EventArgs) Handles btnExport.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
 
 
Screenshot
Export DataTable to CSV (Text) File in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Downloads