title: How to export csv from datagridview only visible column in VB.Net?
body: <p>I'm working on scraping program, the next step from my program after scraping item is generate/export from datagridview into csv file. I have made the save code like below, but i just want to export only a visible column. How can i do like that?</p>
  <pre><code>Private Sub CmdCSV_Click(sender As Object, e As EventArgs) Handles CmdCSV.Click
          Dim sfd As New SaveFileDialog()
          sfd.FileName = &quot;export-csv&quot;
          sfd.Filter = &quot;CSV File | *.csv&quot;
  
          If sfd.ShowDialog() = DialogResult.OK Then
              Using sw As StreamWriter = File.CreateText(sfd.FileName)
                  Dim dgvColumnNames As List(Of String) = DataGridView1.Columns.Cast(Of DataGridViewColumn).ToList().Select(Function(c) c.Name).ToList()
  
                  sw.WriteLine(String.Join(&quot;;&quot;, dgvColumnNames))
  
                  For Each row As DataGridViewRow In DataGridView1.Rows
                      Dim rowData As New List(Of String)
  
                      For Each column As DataGridViewColumn In DataGridView1.Columns
                          rowData.Add(Convert.ToString(row.Cells(column.Name).Value))
                      Next
                      sw.WriteLine(String.Join(&quot;;&quot;, rowData))
                  Next
  
              End Using
          End If
      End Sub
  </code></pre>