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