Solved

C# Export DataGridView

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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