Hi,
I have this code used on my application to export datagridview as csv file.It worked fine but csv doesnt show cell values correclty.See below images and I have pasted my code as well.What can I change on this code to fix this?
Here is the datagridview
This is the exported csv and it shows like this
Here is my code
- public void writeCSV(DataGridView gridIn, string outputFile)
- {
-
-
- if (gridIn.RowCount > 0)
- {
- string value = "";
- DataGridViewRow dr = new DataGridViewRow();
- StreamWriter swOut = new StreamWriter(outputFile);
-
-
-
- for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
- {
- if (i > 0)
- {
- swOut.Write(",");
- }
- swOut.Write(gridIn.Columns[i].HeaderText);
- }
-
- swOut.WriteLine();
-
-
- for (int j = 0; j <= gridIn.Rows.Count - 2; j++)
- {
- if (j > 0)
- {
- swOut.WriteLine();
- }
-
- dr = gridIn.Rows[j];
-
- for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
- {
- if (i > 0)
- {
- swOut.Write(",");
- }
-
- value = dr.Cells[i].Value.ToString();
-
- value = value.Replace(',', ' ');
-
- value = value.Replace(Environment.NewLine, " ");
-
- swOut.Write(value);
- }
- }
- swOut.Close();
- }
- }