C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

Export DataGridView into .csv file


Posted Date: 11 May 2009    Resource Type: Code Snippets    Category: DataGridView
Author: Satish Kumar JMember Level: Diamond    
Rating: 1 out of 5Points: 12



Following example I am trying to save the DataGridView's Data into .csv file

Please create a Windows Application and Add a DataGridView and a button on Form1, and please use following code.

I have explained code inline


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataGridView_ExportToCSV
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//Fetch the Employee details
SqlDataAdapter sqlDa = new SqlDataAdapter("Select EmployeeID, firstname," +
"Lastname from Employees",
"Server=localhost;Database=Northwind;Trusted_Connection=True;");
DataSet ds = new DataSet();
//Fill the DataSet
sqlDa.Fill(ds, "Employees");
//Set the DataSource of DataGrid View
dataGridView1.DataSource = ds.Tables[0];
}

private void button1_Click(object sender, EventArgs e)
{
string strExport = "";
//Loop through all the columns in DataGridView to Set the
//Column Heading
foreach (DataGridViewColumn dc in dataGridView1.Columns)
{
strExport += dc.Name + " ";
}
strExport = strExport.Substring(0, strExport.Length - 3) + Environment.NewLine.ToString();
//Loop through all the row and append the value with 3 spaces
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
foreach (DataGridViewCell dc in dr.Cells)
{
if (dc.Value != null)
{
strExport += dc.Value.ToString() + " ";
}
}
strExport += Environment.NewLine.ToString();
}
strExport = strExport.Substring(0, strExport.Length - 3) + Environment.NewLine.ToString();
//Create a TextWrite object to write to file, select a file name with .csv extention
System.IO.TextWriter tw = new System.IO.StreamWriter("data.csv");
//Write the Text to file
tw.Write(strExport);
//Close the Textwrite
tw.Close();
}
}
}


Please find attached source code.

Hope this helps.


Cheers
SatishKumar J
Microsoft MVP(ASP.NET)


Attachments

  • Source Code (28359-11533-DataGridView_ExportToCSV.zip)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Windows Application  .  Export to .csv  .  Export DataGridView into .csv file  .  DataGridView  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Getting the Selected Value of Combo Box in DataGridView
    Previous Resource: Verify any of the checkbox is selected in a checkbox column of Datagridview
    Return to Discussion Resource Index
    Post New Resource
    Category: DataGridView


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use