title: DataGridView to CSV File
body: <p>I have a VB 2010 Express Project, that has a DataGridView in it, that I am trying to write to a CSV file.</p>
  
  <p>I have the write all working.  But its slow.  Slow = maybe 30 seconds for 6000 rows over 8 columns.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    Private Sub btnExportData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportData.Click
      Dim StrExport As String = ""
      For Each C As DataGridViewColumn In DataGridView1.Columns
          StrExport &amp;= """" &amp; C.HeaderText &amp; ""","
      Next
      StrExport = StrExport.Substring(0, StrExport.Length - 1)
      StrExport &amp;= Environment.NewLine
  
      For Each R As DataGridViewRow In DataGridView1.Rows
          For Each C As DataGridViewCell In R.Cells
              If Not C.Value Is Nothing Then
                  StrExport &amp;= """" &amp; C.Value.ToString &amp; ""","
              Else
                  StrExport &amp;= """" &amp; "" &amp; ""","
              End If
          Next
          StrExport = StrExport.Substring(0, StrExport.Length - 1)
          StrExport &amp;= Environment.NewLine
      Next
  
      Dim tw As IO.TextWriter = New IO.StreamWriter("C:\Test1.CSV")
      tw.Write(StrExport)
      tw.Close()
  End Sub
  </code></pre>
  
  <p>Does anyone know what I can do to speed it up?</p>
  
  <p>thanks</p>