• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Export DataGrid to .csv (vb.net)

Homerboy

Lifer
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:
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 + """", ""))
 
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")
 
I would think it would be here:

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


Edit:
Looks like I was too slow.
 
Back
Top