Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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
Updated 14-Jun-16 9:04am

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)
              {}



C#
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()
 
Share this answer
 
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 Branaghan 20-Nov-12 9:00am    
No problem! Did it work to how you wanted?
A simple way to save also is to use the clipboard and in 3 lines

VB
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)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900