Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 45
  • Last Modified:

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
0
Rowel Virgo
Asked:
Rowel Virgo
  • 3
  • 3
1 Solution
 
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
 
Ryan ChongCommented:
can you show the codes how you bind your DataGridView?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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
 
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
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now