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 22: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 Dnyaneshwar@Pune 719
1 CPallini 245
2 TheRealSteveJudge 240
3 CHill60 235
4 Umer Akram 190
0 Sergey Alexandrovich Kryukov 9,078
1 OriginalGriff 6,771
2 Peter Leow 4,282
3 Zoltán Zörgő 3,809
4 Richard MacCutchan 2,661


Advertise | Privacy | Mobile
Web02 | 2.8.150128.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2015
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