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.