Click here to Skip to main content
12,635,662 members (25,798 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
I have this code to export datagridview rows to csv and it works fine, but when i make some columns of datagridview invisible I dont want them in exported csv file. but I cant get it to work

            StreamWriter sw = new StreamWriter("d://gridview.csv");
            
            for (int i = 0; i < dg1.Columns.Count; i++)
            {
                sw.Write(dg1.Columns[i].HeaderText);
                if (i != dg1.Columns.Count)
                {
                    sw.Write(",");
                }
            }
            
            sw.Write(sw.NewLine);
            
            foreach (DataGridViewRow dr in dg1.Rows)
            {
                
                for (int i = 0; i < dg1.Columns.Count; i++)
                {
                    
                    sw.Write(dr.Cells[i].Value);
                    if (i != dg1.Columns.Count)
                    {
                        sw.Write(",");
                    }
                }
                
                sw.Write(sw.NewLine);
            }

            sw.Flush();
            
            sw.Close();


datagridview isn't bound because its data is cloned from another datagridview
Posted 19-Nov-12 22:10pm
Updated 14-Jun-16 10:04am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Just look for if the column is visible or not, (this should work)
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.aspx[^]
if(dg1.Colums[i].Visible)
              {}



StreamWriter sw = new StreamWriter("d://gridview.csv");

            for (int i = 0; i < dg1.Columns.Count; i++)
            {
              if(dg1.Colums[i].Visible)
              {
                sw.Write(dg1.Columns[i].HeaderText);
                if (i != dg1.Columns.Count)
                {
                    sw.Write(",");
                }
              }
            }

            sw.Write(sw.NewLine);

            foreach (DataGridViewRow dr in dg1.Rows)
            {

                for (int i = 0; i < dg1.Columns.Count; i++)
                {

                    sw.Write(dr.Cells[i].Value);
                    if (i != dg1.Columns.Count)
                    {
                        sw.Write",");
                    }
                }

                sw.Write(sw.NewLine);
            }

            sw.Flush();

            sw.Close()
  Permalink  
Comments
shonezi 20-Nov-12 8:35am
   
I also added your addition to code after foreach (DataGridViewRow dr in dg1.Rows)
thank you a lot!!!!!!!!!!
Rob@Love2Code 20-Nov-12 9:00am
   
No problem! Did it work to how you wanted?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

A simple way to save also is to use the clipboard and in 3 lines


Clipboard.SetDataObject(DataFormats.Text)
DataGridView.SelectAll()
IO.File.WriteAllText(Filename,DataGridView.GetClipBoardContent().GetText.Replace(vbTab, ","), System.Text.Encoding.ASCII)


sets the clipboard content to your desired DataFormat (Text)
select what you want to copy
(note:if you have filtered (invisible?) elements in your DGV they won't be copied)
Write the Clipboard content to the file
(note:here I have replaced the 'Tab' delimiter with a comma to get a CSV file)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 14 Jun 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100