[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

export datagridview to csv - remove last character in row

Posted on 2012-08-15
1
Medium Priority
?
1,135 Views
Last Modified: 2012-08-15
how do i remove the last delimiter in a row when it exports the data to a csv file? i have a ; at the end.

private void btnExport_Click(object sender, EventArgs e)
        {
            //don't append ; at end of row
            System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(@"C:\Projects\Excel\csvExported.csv");
            string strHeader = "";
            for (int i = 0; i < dgvConverted.Columns.Count; i++)
            {
                strHeader += dgvConverted.Columns[i].HeaderText + ";"; //use ; instead of ,
            }
            //streamWriter.WriteLine(strHeader); //don't write headers
            for (int m = 0; m < dgvConverted.Rows.Count; m++)
            {
                string strRowValue = "";
                for (int n = 0; n < dgvConverted.Columns.Count; n++)
                {
                    strRowValue += dgvConverted.Rows[m].Cells[n].Value + ";"; //use ; instead of ,
                }
                streamWriter.WriteLine(strRowValue);
            }
            streamWriter.Close();
        }

Open in new window

0
Comment
Question by:fwstealer
1 Comment
 
LVL 3

Accepted Solution

by:
libby9284 earned 2000 total points
ID: 38297539
use an if/else in the for loop as below

private void btnExport_Click(object sender, EventArgs e)
        {
            //don't append ; at end of row
            System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(@"C:\Projects\Excel\csvExported.csv");
            string strHeader = "";
            for (int i = 0; i < dgvConverted.Columns.Count; i++)
            {
                strHeader += dgvConverted.Columns[i].HeaderText + ";"; //use ; instead of ,
            }
            //streamWriter.WriteLine(strHeader); //don't write headers
            for (int m = 0; m < dgvConverted.Rows.Count; m++)
            {
                string strRowValue = "";
                for (int n = 0; n < dgvConverted.Columns.Count; n++)
                {
                     // *** MODIFIED CODE ***
                      if (n != dgvConverted.Columns.Count - 1)
                      {
                          strRowValue += dgvConverted.Rows[m].Cells[n].Value + ";"; //use ; instead of ,
                      }
                      else 
                      {
                          strRowValue += dgvConverted.Rows[m].Cells[n].Value; //use ; instead of ,
                      }
                     // *** END MODIFICATION ***
                }
                streamWriter.WriteLine(strRowValue);
            }
            streamWriter.Close();
        }

Open in new window

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question