Question on exporting from a datagrid

shogun

New member
Joined
Mar 18, 2009
Messages
2
Programming Experience
Beginner
Hi,

I was wondering if anyone could help me with code to export the information from a datagrid onto a text file(txt) using vb code.

thanks
 
VB.NET:
    Private Sub btnExport_Click() Handles btnExport.Click
        ExportTableToTabDelimitedFile(strFileNameAndPath, myDataset.myTable, True)
    End Sub

    Public Sub ExportTableToTabDelimitedFile(ByVal strFile As String, ByVal dtSource As DataTable, Optional ByVal blnHeader As Boolean = True)

        Dim strBdr As New System.Text.StringBuilder

        Using swExport As New StreamWriter(strFile)
            'Output column names in header row
            If blnHeader = True Then
                For Each colHeader As DataColumn In dtSource.Columns
                    strBdr.Append(colHeader.ColumnName)
                    strBdr.Append(ControlChars.Tab)
                Next colHeader
                swExport.WriteLine(strBdr.ToString)
                strBdr.Remove(0, strBdr.Length)
            End If 'blnHeader

            For Each row As DataRow In dtSource.Rows
                For Each col As DataColumn In dtSource.Columns
                    strBdr.Append(row(col).ToString)
                    strBdr.Append(ControlChars.Tab)
                Next col
                swExport.WriteLine(strBdr.ToString)
                strBdr.Remove(0, strBdr.Length)
            Next row
        End Using 'swExport

    End Sub
 
Back
Top