Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 21:10pm

1 solution

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 at 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 at 20-Nov-12 9:00am
   
No problem! Did it work to how you wanted?

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

  Print Answers RSS
0 OriginalGriff 751
1 George Jonsson 330
2 Garth J Lancaster 290
3 Sergey Alexandrovich Kryukov 274
4 BillWoodruff 252
0 Sergey Alexandrovich Kryukov 10,879
1 OriginalGriff 5,555
2 Maciej Los 5,534
3 CPallini 3,819
4 BillWoodruff 3,594


Advertise | Privacy | Mobile
Web04 | 2.8.141030.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
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