Solved

C# Export DataGridView

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IO.FileSystemWatcher dies when used in Powershell 1 45
Memory Usage 2 50
VB.Net How to Exit Sub - Exit Form??? 5 51
Greetings C# program 17 57
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

14 Experts available now in Live!

Get 1:1 Help Now