By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,781 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,781 IT Pros & Developers. It's quick & easy.

Exporting a datagridview to .csv

jamesd0142
100+
P: 469
Hey how do i go about exporting the contents of a datagridview to a .csv file.

can any point me in the right way?

Thanks
Dec 17 '07 #1
Share this Question
Share on Google+
2 Replies


debasisdas
Expert 5K+
P: 8,127
How you are polulating the datagrid ?
Dec 17 '07 #2

jamesd0142
100+
P: 469
Expand|Select|Wrap|Line Numbers
  1. Private Sub Write(ByVal header As String)
  2.         Dim objStreamWriter As IO.StreamWriter
  3.  
  4.         'Pass the file path and the file name to the StreamWriter
  5.         'constructor.
  6.         'make sure this is a path that you have permissions to save in
  7.  
  8.         objStreamWriter = New IO.StreamWriter(txtPath.Text)
  9.         'Write text.
  10.  
  11.         Dim Str As String
  12.         Dim i As Integer
  13.         Dim j As Integer
  14.  
  15.         Dim headertext = header
  16.         objStreamWriter.WriteLine(headertext)
  17.         For i = 0 To (Me.DGV1.Rows.Count - 2)
  18.             For j = 0 To (Me.DGV1.Columns.Count - 1)
  19.                 'this IF statement stops it from adding a comma after
  20.                 'the last field
  21.                 If j = (Me.DGV1.Columns.Count - 1) Then
  22.                     Str = (Me.DGV1.Rows(i).Cells(j).Value.ToString)
  23.                 Else
  24.                     Str = (Me.DGV1.Rows(i).Cells(j).Value.ToString & ",")    '(Me.DGV1.Rows(i).Cells(j).Text.ToString & ",")
  25.                 End If
  26.                 objStreamWriter.Write(Str)
  27.             Next
  28.             objStreamWriter.WriteLine()
  29.         Next
  30.         'Close the file.
  31.         objStreamWriter.Close()
  32.     End Sub
  33.  
thats the solution i have used... thanks
Dec 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.