Export DataGrid to .csv (vb.net)

Homerboy

Lifer
Mar 1, 2000
30,890
5,001
126
I have the following code which will export my datagrid to .csv. The problem that I have though is that I need the values to be wrapped in double quotes (") so that any values with commas won't be thrown off when opening in Excel etc.

Can somebody help me with this? I feel as if the solution is easy, but I just can't see it this morning...

Thanks in advance.

Code:
 Try
            If Not Directory.Exists("C:\My Files\" & Format(Now(), "yyyy") & "\") Then
                Directory.CreateDirectory("C:\My Files\" & Format(Now(), "yyyy") & "\")
            End If
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

        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("C:\My Files\" & Format(Now(), "yyyy") & "\" & Format(Now(), "yyyy-MM-dd") & " file.csv")
            sw.WriteLine(String.Join(",", headers))
            For Each r In rows
                sw.WriteLine(String.Join(",", r))
            Next
        End Using
 
Last edited:

LevelSea

Senior member
Jan 29, 2013
942
53
91
You could use a different delimiter, such as tab. Excel should handle that fine.
 

Homerboy

Lifer
Mar 1, 2000
30,890
5,001
126
I think I've got it...

Code:
 Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, """" + c.Value.ToString + """", ""))
 

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
Try this

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    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("Jay.txt")
        sw.WriteLine(String.Join(",", headers))
        For Each r In rows
            sw.WriteLine(String.Join(",", r))
        Next
    End Using
    Process.Start("Jay.txt")
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
I would think it would be here:

Code:
If(c.Value IsNot Nothing, """" & c.Value.ToString & """", ""))


Edit:
Looks like I was too slow.
 

Homerboy

Lifer
Mar 1, 2000
30,890
5,001
126
I would think it would be here:

Code:
If(c.Value IsNot Nothing, """" & c.Value.ToString & """", ""))


Edit:
Looks like I was too slow.

:)

I just needed some more coffee before my eyes saw it.
 

Homerboy

Lifer
Mar 1, 2000
30,890
5,001
126
Oops! Almost forgot the headers too!
Code:
Select """" + header.HeaderText + """").ToArray