Solved

C# Export DataGridView

Posted on 2016-11-04
4
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I do Unit Testing with Session ? 1 38
Return array 3 30
Fastest way to get file count in MS Server 2012 3 31
Put File 6 32
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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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