-
May 11th, 2021, 12:48 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Removing first row (headers) of a dgv to a csv file
Following code will create a csv file from a data grid view:
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()
It works but it adds an extra line in beginning of the file representing dgv headers ("Name", "Last Name", "Age", etc.)
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
-
May 11th, 2021, 01:09 PM
#2
Re: Removing first row (headers) of a dgv to a csv file
I'm guessing that you didn't bother to set some breakpoints and step through the code. If you had you would have seen that htese lines:
Code:
For Each C As DataGridViewColumn In DataGridView1.Columns
StrExport &= "" & C.HeaderText & ","
Next
StrExport = StrExport.Substring(0, StrExport.Length - 1)
StrExport &= Environment.NewLine
Are what create that header line, and that deleting them would have solved the problem.
-tg
-
May 11th, 2021, 01:12 PM
#3
Re: Removing first row (headers) of a dgv to a csv file
Well, it looks like your creating it yourself
Code:
For Each C As DataGridViewColumn In DataGridView1.Columns
StrExport &= "" & C.HeaderText & ","
Next
StrExport = StrExport.Substring(0, StrExport.Length - 1)
StrExport &= Environment.NewLine
Try commenting that out.
-
May 11th, 2021, 01:19 PM
#4
Re: Removing first row (headers) of a dgv to a csv file
Take a look at this function:
Code:
Private Function DataGridViewToCsv(grid As DataGridView, Optional ByVal includeHeaders As Boolean = False) As String
Dim builder = New Text.StringBuilder()
If (includeHeaders) Then
builder.AppendLine(String.Join(",", grid.Columns.Cast(Of DataGridViewColumn).Select(Function(cell) cell.HeaderText).ToArray()))
End If
For Each row In grid.Rows.Cast(Of DataGridViewRow)
If (row.IsNewRow) Then
Continue For
End If
builder.AppendLine(String.Join(",", row.Cells.Cast(Of DataGridViewCell).Select(Function(cell) cell.Value?.ToString()).ToArray()))
Next
Return builder.ToString()
End Function
What it does is:
- Declare a new StringBuilder, which is generally more effective then concatenating strings on the fly
- Check if the output should contain the column headers
- if so, then append a line with the column header text, separated by a comma
- Loop over every row
- Check if the currently iterated row is a new row
- if so, then skip it
- Append a line with the cell data, separated by a comma
To use the function, call:
Code:
Dim csv = DataGridViewToCsv(DataGridView1) ' csv without headers
IO.File.WriteAllText(IO.Path.Combine(Application.StartupPath, "file.csv"), csv)
-
May 11th, 2021, 01:29 PM
#5
Thread Starter
Hyperactive Member
Re: Removing first row (headers) of a dgv to a csv file
♥_♥ Thanks to all, my shortest thread ever...
I'm not really into visual studio people, sorry I act like a noob. I'm microcontroller pro. Bye for now
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|