I have some code from here: DataGridView to CSV File Which is looping DataGridView data (source from a web service) to write to a csv. However, i haven't been able to figure out how to handle variables that contain commas. I know they should be commented but am not sure how to work this into the existing loop.
The write must also be able to handle null and boolean fields. Which is why i have struggled to adapt code from elsewhere. Assistance appreciated as always. Existing code as follows.
Dim headers = (From header As DataGridViewColumn In DataGridView1.Columns.Cast(Of DataGridViewColumn)() _
Select header.HeaderText).ToArray
Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
Where Not row.IsNewRow _
Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, """"))
Using sw As New IO.StreamWriter("csv.txt")
sw.WriteLine(String.Join(",", headers))
For Each r In rows
sw.WriteLine(String.Join(",", r))
Next
End Using
Process.Start("csv.txt")
I can't change the delimiter as this will affect subsequent automated use of the file. (output must be csv).