Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export Grid to CSV

Status
Not open for further replies.

bobbyforhire

Technical User
Mar 11, 2008
253
US
I've been looking for a way to do this and i'm guessing that i will just read the datagrid line by line and export it out to a csv.

I'm using UltraGrid and i need to export it out to a CSV, does anyone have any VB code for this as all i can find is c# code.

Thanks!
 
Assuming you have a DataTable as the DataSource of the grid:
Code:
Dim sOutput As String
Dim sw As StreamWriter

sw = New StreamWriter("C:\Path\to\file.csv")

For Each dr As DataRow In DataTable1.Rows

    sOutput = ""

    For c As Integer = 0 to DataTable1.Columns.Count - 1
        sOutput &= dr.Item(c) & ","
    Next

    'remove last comma
    sOutput = sOutput.SubString(0, sOutput.Length - 1)

    'write line to text file
    sw.WriteLine(sOutput)

Next

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Well, i'm having to manually load the .csv as i can't find a way to setup a schema to use a csv.

cnCSV = New OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & DataFile.Directory.FullName & ";")


So it's just whatever is on the grid that i made changes to i wanted to save off to the csv and next time it loads it will go from that csv here on out.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top