Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 25th, 2006, 03:52 AM
Registered User
Join Date: Oct 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I export data from a DataViewGrid to a CSV


I am new to VB.net programming and was wondering if anybody could help, I am using a DataViewGrid to hold data from an Access Database, but I am wanting to export the data from the DataGridView to a CSV file via a command button.

Does anybody have any ideas on how I can acheive this, or if you could send me a sample of VB.net code, would be really appreciated.

Many thanks.

Old November 22nd, 2006, 11:57 AM
Authorized User
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196

Hello Paul,

Here is a sample procedure that will output the contents of a DataGridView to a CSV file.

'* Add the following to your Imports section of your form:
'* Imports System.io

Private Sub subExportDGVToCSV(ByVal strExportFileName As String, _
    ByVal DataGridView As DataGridView, _
    Optional ByVal blnWriteColumnHeaderNames As Boolean = False, _
    Optional ByVal strDelimiterType As String = ",")

    '* Parameters Description:
    '* --------------------------------------------------------------
    '* strExportFileName = The name of the file to export to.

    '* DataGridView = The name of the DataGridView on your form.

    '* blnWriteColumnHeaderNames = YES/NO for writing the column
    '* names as the first line of the CSV file. This will cause
    '* programs like Excel to argue but still open the file.

    '* strDelimiterType = The type of delimiter you want to use.
    '* Examples: TAB (vbTab) or Comma (",")
    '* --------------------------------------------------------------

    '* Create a StreamWriter object to open and write contents
    '* of the DataGridView columns and rows.
    Dim sr As StreamWriter = File.CreateText(strExportFileName)

    '* Create a variable to hold the delimiter type
    '* (i.e., TAB or comma or whatever you choose)
    '* The default for this procedure is a comma (",").
    Dim strDelimiter As String = strDelimiterType

    '* Create a variable that holds the total number of columns
    '* in the DataGridView.
    Dim intColumnCount As Integer = DataGridView.Columns.Count - 1

    '* Create a variable to hold the row data
    Dim strRowData As String = ""

    '* If the CSV file will have column names then write that data
    '* as the first line of the file.
    If blnWriteColumnHeaderNames Then

    '* Interate through each column and get/write the column name.
        For intX As Integer = 0 To intColumnCount

            '* The IIf statement will not put a delimiter after the
            '* last value added.

            '* The Replace function will remove the delimiter
            '* from the field data if found.
            strRowData += Replace(DataGridView.Columns(intX).Name, strDel imiter, "") & _
                IIf(intX < intColumnCount, strDelimiter, "")

        Next intX

        '* Write the column header data to the CSV file.

    End If '* If blnWriteColumnHeaderNames

    '* Now collect data for each row and write to the CSV file.
    '* Loop through each row in the DataGridView.
    For intX As Integer = 0 To DataGridView.Rows.Count - 1

        '* Reset the value of the strRowData variable
        strRowData = ""

        For intRowData As Integer = 0 To intColumnCount

            '* The IIf statement will not put a delimiter after the
            '* last value added.

            '* The Replace function will remove the delimiter
            '* from the field data if found.
            strRowData += Replace(DataGridView.Rows(intX).Cells(intRowData). Value, strDelimiter, "") & _
                IIf(intRowData < intColumnCount, strDelimiter, "")

        Next intRowData

        '* Write the row data to the CSV file.

    Next intX

    '* Close the StreamWriter object.

    '* You are done!

End Sub

Here is an example of calling the above procedure:

Call subExportDGVToCSV("C:\Test.csv", Me.dg, False, ",")

Best Regards,
Earl Francis

Similar Threads
Thread Thread Starter Forum Replies Last Post
Csv Export Coby Access VBA 4 July 25th, 2007 08:19 AM
Export To .csv File paul31Hampshire Access VBA 18 June 26th, 2006 11:30 AM
Export data in to csv file from asp.netpage madhusrp ASP.NET 1.0 and 1.1 Professional 1 June 9th, 2006 09:15 AM
More Export.csv mayhem Morris Access 1 February 24th, 2004 12:52 PM
Export csv grahampinkney Access 3 February 21st, 2004 07:35 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.