2

Possible Duplicate:
Very simple C# CSV reader

In my application the user can export and csv files from and to a datagridview, this works okay but there is some minor problem.

As you know csv files use the comma sign to sepperate items.

Sometimes one of the cells has a comma sign for example (2,5 hours).

When importing the csv it thinks that the comma sign between 2,5 is a seperator, because of this it will put 2 in a cell and 5 in the next one.

How can i fix this problem.

This is my csv for importing csv's

dataGridView1.Rows.Clear();
        try
            {
                if (openFileCsv.ShowDialog() == DialogResult.OK)
                {
                    string csvPath = openFileCsv.FileName;

                    if (System.IO.File.Exists(csvPath))
                    {
                        System.IO.StreamReader fileReader = new System.IO.StreamReader(csvPath, false);


                        //Reading Data
                        while (fileReader.Peek() != -1)
                        {
                            fileRow = fileReader.ReadLine();
                            fileDataField = fileRow.Split(',');
                            dataGridView1.Rows.Add(fileDataField);
                        }
                        fileReader.Dispose();
                        fileReader.Close();
                    }
                    else
                    {
                        MessageBox.Show("CSV Bestand niet gevonden.");
                    }
                }

                DataLoaded = true;

            }

This is my code for exporting the csv.

if (saveFileCsv.ShowDialog() == DialogResult.OK)
            {
                string CsvFpath = saveFileCsv.FileName;

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

            int countColumn = dataGridView1.ColumnCount - 1;

            int iColCount = dataGridView1.Columns.Count;

            foreach (DataGridViewRow dataRowObject in dataGridView1.Rows)
            {
                //Checking for New Row in DataGridView
                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();
                    }

                    //Writing Data Rows in File
                    csvFileWriter.WriteLine(dataFromGrid);
                }
            }


            csvFileWriter.Flush();
            csvFileWriter.Close();
        }
        catch (Exception exceptionObject)
        {
            MessageBox.Show(exceptionObject.ToString());
        }
Community
  • 1
  • 1
PandaNL
  • 828
  • 5
  • 18
  • 40

2 Answers2

5

Try to use semicolon separator, or quotation marks with comma (","):

fileRow = fileReader.ReadLine();
fileDataField = fileRow.Replace("\",\"", "\r").Split('\r');
dataGridView1.Rows.Add(fileDataField);
3

You can either use another character, other than a comma to separate the variables, or you could enclose the values in quotes.

Have a look at this.

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269