Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Make Double quotes " " as CSV Columns

$
0
0

Hi, let straight to the point,

I have table like this (in datagridview) - Read CSV file and load into datagridview

I want the output like in CSV file like

"Column 0","Column 1","Column 2",Column 3","Column 4","Column 5",

"Data Column 0","Data Column 1","Data Column 2","DAT=EX1, Test=MMM, MAKE=YYYY","117","5",

"Data Column 0","Data Column 1","Data Column 2","DAT=EX1, Test=MMM, MAKE=YYYY","121","4",

"Data Column 0","Data Column 1","Data Column 2","DAT=EX1, Test=MMM, MAKE=YYYY","120","5",

"Data Column 0","Data Column 1","Data Column 2","DAT=EX1, Test=MMM, MAKE=YYYY","123","7",

I used the below code but not working

    string value = "";
                DataGridViewRow dr = new DataGridViewRow();
                StreamWriter swOut = new StreamWriter(sfd.FileName, false, Encoding.Default); 

                for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++)
                {
                    if (j > 0)
                    {
                        swOut.WriteLine();
                    }

                    dr = dataGridView1.Rows[j];

                    for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
                    {
                        if (i > 0)
                        {
                            swOut.Write(",");
                        }

                        value = Convert.ToString(dr.Cells[i].Value);
                        value = value.Replace(',', ' ');
                        value = value.Replace(Environment.NewLine, " ");

                        swOut.Write(value);
                    }
                }
                swOut.Close();

Thanks in advance...


Viewing all articles
Browse latest Browse all 31927

Trending Articles