Hi There,

I have a datagrid table with 5 columns and as many rows as the user enters.

Is there a simple way to export the contents of the table to a CSV file?

The columns are named; a,b,c,d and e

Im using VB 2005.

Mant Thanks

Alex

Hi,

You can try something like this:

Private Sub button1_Click(sender As Object, e As EventArgs)
	Dim strExport As String = ""
	'Loop through all the columns in DataGridView to Set the 
	'Column Heading
	For Each dc As DataGridViewColumn In dataGridView1.Columns
		strExport += dc.Name + "   "
	Next
	strExport = strExport.Substring(0, strExport.Length - 3) & Environment.NewLine.ToString()
	'Loop through all the row and append the value with 3 spaces
	For Each dr As DataGridViewRow In dataGridView1.Rows
		For Each dc As DataGridViewCell In dr.Cells
			If dc.Value IsNot Nothing Then
				strExport += dc.Value.ToString() & "   "
			End If
		Next
		strExport += Environment.NewLine.ToString()
	Next
	strExport = strExport.Substring(0, strExport.Length - 3) & Environment.NewLine.ToString()
	'Create a TextWrite object to write to file, select a file name with .csv extention
	Dim tw As System.IO.TextWriter = New System.IO.StreamWriter("data.csv")
	'Write the Text to file
	tw.Write(strExport)
	'Close the Textwrite
	tw.Close()
End Sub

I haven't tested it.

Hi, thanks for that;

For Each dc.Value As DataGridViewCell In dr.Cells
            Next
            If dc.Value IsNot Nothing Then strExport += dc.Value.ToString() & "   "

For some reason its telling me the variable dc.Value is undeclared, but recognizes the previous dc.Name, if i remove the above code the rest works and creates a csv file with the column headers but with obviously nothing in them.

Any ideas?

Cheers

Alex

Edited 5 Years Ago by Alexkid: n/a

Try to remove "Value" from the if statement:

If dc IsNot Nothing Then
	strExport += dc.Value.ToString() & "   "
End If

Anyway, here is my code of putting dgv`s content to a csv file:

Private Sub buttonSave_Click(sender As Object, e As EventArgs)
	Dim sbCSV As New StringBuilder()
	Dim intColCount As Integer = dt.Columns.Count
	For Each dr As DataRowView In dt.DefaultView
		For x As Integer = 0 To intColCount - 1
			sbCSV.Append(dr(x).ToString())
			If (x + 1) <> intColCount Then
				sbCSV.Append(",")
			End If
		Next
		sbCSV.Append(vbLf)
	Next
	Using sw As New StreamWriter("c:\csv\table1.csv")
		sw.Write(sbCSV.ToString())
	End Using
End Sub

Thanks for that.

I'm still getting "name dt is undeclared" in both cases.

Should i be importing anything? do i have to name the table "dt" anywhere. The rows (dr) seem to have no problem. Also i can't find the attribute "Value" for dt in the drop down box?

Thanks

Alex

Hi,

You can import:

Imports System.Data
Imports System.Data.SqlClient

I hope it helps.

Thanks,

Still no joy, I'm still getting "name dt is undeclared"

I've tried adding

Dim dt As New DataTable

Which sorts the promblem but doesn't put anything in the created csv file.

I'm really stuck so any help would be appriciated,

I can create the csv file and add the columns in ok but i can seem to put the cell contents in (the "dt / dc" variable stuff).

Has anyone else tried/tested the above code?

I'm fairly new to VB so ideas would be greatly appriciated.

Many Thanks

Alex

Edited 5 Years Ago by Alexkid: n/a

doh!!, found the problem, dt was out of scope in the for statement.

Thanks for your help guys.

Heres the complete solution which will create the CSV file in the Debug folder:

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim strExport As String = ""
        For Each dc As DataGridViewColumn In DataGridView1.Columns
            strExport += dc.Name + "   "
        Next
        strExport = strExport.Substring(0, strExport.Length - 3) & Environment.NewLine.ToString()

        For Each dr As DataGridViewRow In DataGridView1.Rows
            For Each dc As DataGridViewCell In dr.Cells
                If dc.Value IsNot Nothing Then
                    strExport += dc.Value.ToString() & "   "
                End If
            Next
            strExport += vbCrLf
        Next
        strExport += Environment.NewLine.ToString()
        strExport = strExport.Substring(0, strExport.Length - 3) & Environment.NewLine.ToString()
        Dim tw As System.IO.TextWriter = New System.IO.StreamWriter("data.csv")
        tw.Write(strExport)
        tw.Close()
    End Sub

Edited 5 Years Ago by Alexkid: n/a

Hi AlexKid,

Glad to here that you found the problem.
Like I wrote you, I didn't tested it but was pretty sure that my code worked.

Grtz,

Luc001

Thanks very much...But in CSV file i got Whole DataGridview data even click on single Row...Please help me...

This question has already been answered. Start a new discussion instead.