DataGridView to CSV File

后端 未结 1 1732
北恋
北恋 2020-12-15 01:25

I have a VB 2010 Express Project, that has a DataGridView in it, that I am trying to write to a CSV file.

I have the write all working. But its slow. Slow = maybe

相关标签:
1条回答
  • 2020-12-15 02:17

    Don't you just love it when you spend hours searching, then post a question, and a few minutes later find the answer yourself?

    This did the trick for me:

    Dim headers = (From header As DataGridViewColumn In DataGridView1.Columns.Cast(Of DataGridViewColumn)() _
                  Select header.HeaderText).ToArray
    Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
               Where Not row.IsNewRow _
               Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, ""))
    Using sw As New IO.StreamWriter("csv.txt")
        sw.WriteLine(String.Join(",", headers))
        For Each r In rows
            sw.WriteLine(String.Join(",", r))
        Next
    End Using
    Process.Start("csv.txt")
    
    0 讨论(0)
提交回复
热议问题