Export Data From Datagridview to CSV File

i have a datagridview with data from database SQL. I want to export it to csv file., I have a code but after exporting the file has no data. What's wrong with this code?

Database
class.vb code

Imports System.IO

Public Class classCSV

    Function export(ByVal DT As DataTable, ByVal fullpath As String, ByVal delimiter As String) As Boolean

        Dim intI, intC As Integer
        Dim strValue As String
        Dim strRowValue As String
        Dim enc As System.Text.Encoding
        Dim Qualifier As String = """"
        Dim row As DataRow

        enc = System.Text.Encoding.GetEncoding(1252)
        Dim swriter As New StreamWriter(fullpath, False, enc)
        intC = DT.Columns.Count

        strRowValue = ""
        strValue = ""

        For intI = 0 To intC - 1
            strValue = DT.Columns(intI).ColumnName
            strValue = strValue.Replace(Qualifier, Qualifier & Qualifier)

            strRowValue = strRowValue & Qualifier & strValue & Qualifier

            If intI < intC - 1 Then
                strRowValue = strRowValue & delimiter
            End If
        Next

        swriter.Write(strRowValue)
        swriter.WriteLine()

        For Each row In DT.Rows
            strRowValue = ""
            strValue = ""

            For intI = 0 To intC - 1
                strValue = row.Item(intI).ToString
                strValue = strValue.Replace(Qualifier, Qualifier & Qualifier)

                strRowValue = strRowValue & Qualifier & strValue & Qualifier

                If intI < intC - 1 Then
                    strRowValue = strRowValue & delimiter
                End If
            Next

            swriter.Write(strRowValue)
            swriter.WriteLine()
        Next

        swriter.Close()

    End Function
End Class

Open in new window


Export CMDButton
    Private MDT As New DataTable
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim Export As New classCSV
        Export.export(MDT, "d:\ESD RECORDS\ESD-Record.csv", ",")
    End Sub

Open in new window


The file created is empty

File
Rowel VirgoVisual Studio .NETAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
the reason it's empty is because you didn't populate the MDT with the DataGridView's binding source.

hence, try this:

Dim Export As New classCSV

        Dim bindingSource = DirectCast(YourDataGridView.DataSource, BindingSource)
        Dim ds = DirectCast(bindingSource.DataSource, DataSet)
        MDT = ds.Tables(0)

        Export.export(MDT, "d:\ESD RECORDS\ESD-Record.csv", ",")

Open in new window

0
Rowel VirgoVisual Studio .NETAuthor Commented:
System.InvalidCastException: 'Unable to cast object of type 'System.Data.DataTable' to type 'System.Windows.Forms.BindingSource'.'

Error sir
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
can you show the codes how you bind your DataGridView?
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
you may try this if your binding source is a DataTable.

(code not tested)
Dim bindingSource = DirectCast(YourDataGridView.DataSource, BindingSource)
       MDT = DirectCast(bindingSource.DataSource, DataTable)

Open in new window


the one I'm showing above was binding to a DataSet
1
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Is your DataGridView bound to a Datatable? If so you can do this:

Dim MDT As Datatable
MDT = YourDGV.DataSource

I do this with DGVs I know are bound to Datatables, and it seems to work.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rowel VirgoVisual Studio .NETAuthor Commented:
This is how I populate my DataGridView from SQL

        Dim cmd2 As New MySqlCommand
        Dim myDA2 As New MySqlDataAdapter(cmd2)
        Dim myDT2 As New DataTable
        cmd2.Connection = conn
        cmd2.CommandText = "SELECT * FROM msd.esd_reco ORDER BY Date DESC"
        myDA2.Fill(myDT2)
        DataGridView1.DataSource = myDT2

Open in new window

0
Rowel VirgoVisual Studio .NETAuthor Commented:
This one works sir, Thank you so much, Thanks everyone.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
converter

From novice to tech pro — start learning today.