Solved

C# Export DataGridView

Posted on 2016-11-04
4
39 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 39

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 69

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now