Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
StreamWriter streamWriter = new StreamWriter("" + TxtDestnation.Text + "");
            string strHeader = "";

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

                strHeader += dataGridView1.Columns[i].HeaderText + ",";

            }

            streamWriter.WriteLine(strHeader);
            for (int m = 0; m < dataGridView1.Rows.Count; m++)
            {

                string strRowValue = "";

                for (int n = 0; n < dataGridView1.Columns.Count; n++)
                {

                    strRowValue += dataGridView1.Rows[m].Cells[n].Value + ",";

                }
                streamWriter.WriteLine(strRowValue);
            }

My problem is i am get the csv file like this


EmployeeCode,EmployeeName,AttendanceDate,
46,Swathi B,5/5/2012 12:00:00 AM,

End of the row i don't want comma(,)

please help me
Posted

1 solution

Add one line of code after creating each row to remove last comma(,) as follows:

C#
StreamWriter streamWriter = new StreamWriter("" + TxtDestnation.Text + "");
            string strHeader = "";

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

                strHeader += dataGridView1.Columns[i].HeaderText + ",";

            }

            streamWriter.WriteLine(strHeader);
            for (int m = 0; m < dataGridView1.Rows.Count; m++)
            {

                string strRowValue = "";

                for (int n = 0; n < dataGridView1.Columns.Count; n++)
                {

                    strRowValue += dataGridView1.Rows[m].Cells[n].Value + ",";

                }
                strRowValue = strRowValue.Substring(0, strRowValue.Length - 1);  
                streamWriter.WriteLine(strRowValue);
            }
 
Share this answer
 
v2
Comments
praveen kadimi 23-Nov-12 5:15am    
Thank you very much its working
member60 23-Nov-12 6:52am    
my 5!

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