troubleshooting Question

VB.Net Export DataGridView to CSV but with Current Column Order

Avatar of spen_lang
spen_lang asked on
Visual Basic.NET.NET Programming
4 Comments1 Solution43 ViewsLast Modified:

I have created the following code to export a datagridview to CSV, however the user is able to reorder the column position.

For example original layout is:

ColA, ColB, ColC

User changes the column orders:

ColC, ColA, ColB

But export to CSV is still in the original column order:

ColA, ColB, ColC

How do I export the datagridview to CSV as it looks on screen?

 Thanks, Greg
Private Sub exportCSV(ByRef sExportFileName As String)

            Dim sCSV As New List(Of String)

            ' get column headers
            Dim sColHeaders As String = ""

            For Each col As DataGridViewColumn In dgv.Columns

                If col.Visible = True Then

                    Dim sCol As String = ""

                    sCol = col.HeaderText.ToString

                    If sCol.Contains(",") Then
                        sCol = """" + sCol + """"
                    End If

                    sColHeaders = sColHeaders + sCol + ","

                End If

            sColHeaders = sColHeaders.Substring(0, sColHeaders.Length - 1)

            ' get row cell values
            For Each row As DataGridViewRow In dgv.Rows

                Dim sRow As String = ""

                For Each cell As DataGridViewCell In row.Cells

                    If cell.Visible = True Then

                        Dim sValue As String = ""

                        If Not cell.Value Is Nothing Then
                            sValue = cell.Value.ToString
                            sValue = ""
                        End If

                        If sValue.ToString.Contains(",") Then
                            sValue = """" + sValue + """"
                        End If

                        If sValue.ToString.ToUpper = "TRUE" Then
                            sValue = "Yes"
                        End If

                        If sValue.ToString.ToUpper = "FALSE" Then
                            sValue = "No"
                        End If

                        sRow = sRow + sValue + ","

                    End If

                sRow = sRow.Substring(0, sRow.Length - 1)

            ' write data to csv file
            Using sw As New IO.StreamWriter(sExportFileName, False, System.Text.Encoding.UTF8)


                For Each s As String In sCSV

            End Using

            If MessageBox.Show("CSV file created, open CSV file?", "Export Complete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString, "CSV Export Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
Naitik Gamit
Senior Software Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2015

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros