Solved

C# Export DataGridView

Posted on 2016-11-04
4
63 Views
Last Modified: 2016-11-07
Could anybody help how to export DataGridView to a tab delimited text file in C#?
0
Comment
Question by:ybt
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 41874925
Based off of:
http://www.aspsnippets.com/Articles/Export-Windows-Forms-WinForms-DataGridView-to-CSV-using-C-and-VBNet.aspx

private void btnExport_Click(object sender, EventArgs e)
{
    //Build the CSV file data as a Comma separated string.
    string tab = string.Empty;
 
    //Add the Header row for tab file.
    foreach (DataGridViewColumn column in dataGridView1.Columns)
    {
        tab += """" + column.HeaderText + """" + '\t';
    }
 
    //Add new line.
    tab += Environment.NewLine;
 
    //Adding the Rows
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        foreach (DataGridViewCell cell in row.Cells)
        {
            //Add the Data rows.
            tab += """" + cell.Value.ToString() + """" + '\t'  ;
        }
 
        //Add new line.
        tab += Environment.NewLine;
    }
 
    //Exporting to TSV.
    string folderPath = "C:\\TSV\\";
    File.WriteAllText(folderPath + "DataGridViewExport.tsv", tab);
}

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41875394
You can also surely cast the the DataSource property of the grid into a datatable and use this trick: http://emoreau.com/Entries/Articles/2009/04/Using-LINQ-and-XML-Literals-to-transform-a-DataTable-into-a-HTML-table.aspx
0
 

Expert Comment

by:Aishwarya Shiva Pareek
ID: 41875451
Try this:

var sb = new StringBuilder();

var headers = dataGridView1.Columns.Cast<DataGridViewColumn>();
sb.AppendLine(string.Join(",", headers.Select(column => "\"" + column.HeaderText + "\"").ToArray()));

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    var cells = row.Cells.Cast<DataGridViewCell>();
    sb.AppendLine(string.Join(",", cells.Select(cell => "\"" + cell.Value + "\"").ToArray()));
}
System.IO.StreamWriter file = new System.IO.StreamWriter(@"\YourFile.csv");
file.WriteLine(sb.ToString());

Open in new window

0
 

Author Closing Comment

by:ybt
ID: 41877809
Thank you, it works, others advises is fine, but about coma separated and my particular need was tab delimited file.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question