Question Export All Data in Datagridview to Text File

ryoka012

Active member
Joined
Oct 13, 2011
Messages
32
Programming Experience
Beginner
Hi Guys can anynone help me in exporting all the data in datagrid to text file.

Here is my code.


Browsing the Data file.
VB.NET:
 Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
        Dim odlg As New Windows.Forms.OpenFileDialog()
        odlg.Title = "Select DBF File"
        odlg.InitialDirectory = "E:\OPus"
        odlg.FileName = txtFileName.Text
        odlg.Filter = "DBF Files(*.dbf)|*.dbf|All Files(*.*)|*.*"
        odlg.FilterIndex = 1
        odlg.RestoreDirectory = True
        If odlg.ShowDialog() = DialogResult.OK Then
            txtFileName.Text = odlg.FileName
            FileNameToMove = odlg.SafeFileName
            Application.DoEvents()


        End If
        GetDBFDatatoGridView(txtFileName.Text)
    End Sub

Populate Datagrid from Datafile
VB.NET:
 Public Sub GetDBFDatatoGridView(ByVal strFileName As String)
        Dim DBFcon As OdbcConnection
        Dim DBFconnectionString As String


        DBFconnectionString = "Driver={Microsoft dBASE Driver (*.dbf)};" + "Driverid=277;" + "Dbq=" + System.IO.Path.GetFullPath(strFileName).Replace(System.IO.Path.GetFileName(strFileName), "")
        DBFcon = New OdbcConnection(DBFconnectionString)
        Dim strDBFQuery As String = "Select * from [" + System.IO.Path.GetFileName(strFileName) + "]"
        Try
            DBFcon.Open()
            Dim DBFcmd = DBFcon.CreateCommand
            DBFcmd.CommandText = strDBFQuery
            DBFcmd.CommandType = CommandType.Text
            Dim dr As OdbcDataReader = DBFcmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(dr)
            DataGridView.DataSource = dt
            'SaveOpusOne()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If DBFcon IsNot Nothing Then
                DBFcon.Close()
                DBFcon.Dispose()
            End If
        End Try
    End Sub

Generate Raw File
VB.NET:
 Dim numCols As Integer = DataGridView.ColumnCount
        Dim numRows As Integer = DataGridView.RowCount - 1
        Dim Year As String = DateTime.Now.ToString("yy")
        Dim Month As String = DateTime.Now.ToString("MM")
        Dim Day As String = DateTime.Now.ToString("dd")
        Dim Hour As String = DateTime.Now.ToString("HH")
        Dim Minutes As String = DateTime.Now.ToString("mm")
        Dim Seconds As String = DateTime.Now.ToString("ss")


        Dim GenFileName = Year + Month + Day + Hour + Minutes + Seconds


        strDestinationFile = "E:\Opus\OutputData\" + GenFileName + ".gen"
        Dim tw As TextWriter = New StreamWriter(strDestinationFile)

[B][COLOR=#ff0000]' Here is where i populate my may data.[/COLOR][/B]


        For count As Integer = 0 To numRows - 1
            For count2 As Integer = 0 To numCols - 1
                tw.Write(DataGridView.Rows(count).Cells(count2).Value)
                If (count2 <> numCols) Then
                    tw.Write(""",""")
                End If
            Next
            tw.WriteLine()
        Next
        tw.Close()
 
Why not use the built in Xml saving with DataTable.WriteXml method? DataTable that was assigned DataSource can be retrived from same property also.
 
Thanks JohnH for the reply


How can i use DataTable.WriteXml in my code.
Can you help me with this.

Thanks again

 
DirectCast(theDataGridView.DataSource, DataTable).WriteXml(theDestinationFilepath)
 
Back
Top