Following code will create a csv file from a data grid view:
It works but it adds an extra line in beginning of the file representing dgv headers ("Name", "Last Name", "Age", etc.)Code:Dim StrExport As String = "" For Each C As DataGridViewColumn In DataGridView1.Columns StrExport &= "" & C.HeaderText & "," Next StrExport = StrExport.Substring(0, StrExport.Length - 1) StrExport &= 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 &= "" & C.Value.ToString & "," End If Next StrExport = StrExport.Substring(0, StrExport.Length - 1) StrExport &= Environment.NewLine Next Dim tw As IO.TextWriter = New IO.StreamWriter(Application.StartupPath & "\file.csv") tw.Write(StrExport) tw.Close()
Which cause damage next time I call/open this file.
How can I fix it? I added more negative numbers in for loop and increased starting point, but VS crashed... xd




Reply With Quote
