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?
 
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
 
Ryan ChongCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Ryan ChongCommented:
can you show the codes how you bind your DataGridView?
0
 
Ryan ChongCommented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.