Click here to Skip to main content
12,063,242 members (53,087 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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 22-Nov-12 23:06pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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);
            }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160204.4 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2016
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