Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Export data from gridview to csv file

Export data from gridview to csv file

Scheduled Pinned Locked Moved C#
csharpcssquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gckumar
    wrote on last edited by
    #1

    How do i export the data from grid view to csv file in c#

    D C 2 Replies Last reply
    0
    • G gckumar

      How do i export the data from grid view to csv file in c#

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Does this works? StreamWriter oWriter = new StreamWriter("CSV File Path", false); // First we will write the headers. for(int i = 0; i < oGridView.Columns.Count; i++) { oWriter.Write(oGridView.Columns[i].HeaderText); if(i < oGridView.Columns.Count - 1) { oWriter.Write(","); } } oWriter.Write(oWriter.NewLine); // Now write all the rows. foreach(DataGridViewRow dr in oGridView.Rows) { for(int i = 0; i < oGridView.Columns.Count; i++) { oWriter.Write(dr.Cells[i].Value.ToString()); if(i < oGridView.Columns.Count - 1) { oWriter.Write(","); } } oWriter.Write(oWriter.NewLine); } oWriter.Close();

      Avoid Google. Use Blackle[^]

      modified on Wednesday, May 14, 2008 4:24 AM

      1 Reply Last reply
      0
      • G gckumar

        How do i export the data from grid view to csv file in c#

        C Offline
        C Offline
        ctlqt12
        wrote on last edited by
        #3

        Hope this will help you :

        private void OnExportGridToCSV(object sender, System.EventArgs e)
        {
        	// Create the CSV file to which grid data will be exported.
        	StreamWriter sw = new StreamWriter(Server.MapPath("~/GridData.txt"), false);
        
        	// First we will write the headers.
        	DataTable dt = m_dsProducts.Tables[0];
        	int iColCount = dt.Columns.Count;
        	for(int i = 0; i < iColCount; i++)
        	{
        		sw.Write(dt.Columns[i]);
        		if (i < iColCount - 1)
        		{
        			sw.Write(",");
        		}
        	}
        	sw.Write(sw.NewLine);
        
        	// Now write all the rows.
        	foreach (DataRow dr in dt.Rows)
        	{
        		for (int i = 0; i < iColCount; i++)
        		{
        			if (!Convert.IsDBNull(dr[i]))
        			{
        				sw.Write(dr[i].ToString());
        			}
        			if ( i < iColCount - 1)
        			{
        				sw.Write(",");
        			}
        		}
        		sw.Write(sw.NewLine);
        	}
        	sw.Close();
        }
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups