Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 22: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 at 23-Nov-12 5:15am
   
Thank you very much its working
member60 at 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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 422
1 sanket saxena 355
2 Abhinav S 258
3 OriginalGriff 250
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,704
1 OriginalGriff 5,000
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid