Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:24:17+00:00 2026-06-13T06:24:17+00:00

I am trying to export to CSV, however the problem I am facing is

  • 0

I am trying to export to CSV, however the problem I am facing is when the data contained within the grid view contains comma’s. Since the a “,”, is used as the delimiter this is causing all sorts of issues within the csv. Below is the code. Any ideas how I can avoid this?

  try
        {
            System.IO.StreamWriter csvFileWriter = new StreamWriter(CsvFpath, false);

            string columnHeaderText = "";

            int countColumn = dataGridViewLogging.ColumnCount - 1;

            if (countColumn >= 0)
            {
                columnHeaderText = dataGridViewLogging.Columns[0].HeaderText;
            }

            for (int i = 1; i <= countColumn; i++)
            {
                columnHeaderText = columnHeaderText + ',' + dataGridViewLogging.Columns[i].HeaderText;
            }


            csvFileWriter.WriteLine(columnHeaderText);

            foreach (DataGridViewRow dataRowObject in dataGridViewLogging.Rows)
            {
                if (!dataRowObject.IsNewRow)
                {
                    string dataFromGrid = "";

                    dataFromGrid = dataRowObject.Cells[0].Value.ToString();

                    for (int i = 1; i <= countColumn; i++)
                    {
                        dataFromGrid = dataFromGrid + ',' + dataRowObject.Cells[i].Value.ToString();

                        csvFileWriter.WriteLine(dataFromGrid);
                    }
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:24:19+00:00Added an answer on June 13, 2026 at 6:24 am

    Use an escape character, and use a parser that understands how to deal with escapes. You can quickly write an Escape() extension and an Unescape() extension….

    public static class ExtentionClass
    {
        public static string Escape(this string str)
        {
              return str.Replace("\\","\\\\").Replace(",","\\,");
        }
        public static string Unescape(this string str)
        {
              return str.Replace("\\\\","\\").Replace("\\,",",");
        }
    }
    

    now you can change your line to…

    dataFromGrid = dataFromGrid + ',' + dataRowObject.Cells[i].Value.ToString().Escape();
    

    But, you are probably wanting to view the output in Excel, then \ doesn’t work… It needs to be double quoted.

    ...
        public static string Escape(this string str)
        {
            return "\"" + str.Replace("\"","\"\"") + "\"";
        }
    

    You can read more at…
    CSV for Excel, Including Both Leading Zeros and Commas

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to export CSV in SSRS 2008. I am getting my data,
I'm trying to export data to a csv file. It should contain a header
I'm trying to export/import data in .csv format using SQLDeveloper. The source and destination
I am trying to export data from my MySQL database to multiple CSV files
I am trying to export a list of table to comma-separated value(CSV) .but not
I'm trying to export data from SQL Server into CSV format. I have a
I'm having trouble trying to export saved data from CoreData to a CSV file.
I am trying to export a data.frame to a csv with utf-8 encoding. I
I am trying to export cgridview data (current search results) to a CSV by
I am trying to export the following data in the query from ms sql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.