I am working on a project to where a datagridview is populated by text boxes. This information contains Name, DOB, Phone number, address, etc. I am then writing the data to a text file.
I am currently using this code:
ReportWriter.WriteLine("")
For Each currentrow As DataGridViewRow In Me.DataGridView1.Rows
For Each currentcolumn As DataGridViewCell In currentrow.Cells
ReportWriter.Write(currentcolumn.Value & " ")
Next
ReportWriter.WriteLine()
Next
From I get a readout in the format of:
Fred 1/1/1111 1231231234 1234 lane'
Is there any way I can specify text to put in front of each column readout such as "Name:"?
Example:
Name: fred DOB: 1/1/1111 Phone: 1231234567
Address: 1234 lane
Like so:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Windows.Forms
'''''''''''''''''
ReportWriter.WriteLine()
Dim cols As List(Of DataGridViewColumn) = Me.DataGridView1.Columns.Cast(Of DataGridViewColumn).ToList()
For Each row As DataGridViewRow In Me.DataGridView1.Rows
For Each cell As DataGridViewCell In row.Cells
Dim col As DataGridViewColumn = cols(cell.ColumnIndex)
ReportWriter.Write(col.Name)
ReportWriter.Write(": """)
ReportWriter.Write(Replace(col.Value, vbCrLf, " ")) ' Escape line-breaks in the value too.
ReportWriter.Write(""", ")
Next
ReportWriter.WriteLine()
Next
BTW, don't use the &
string-concatenation operator when you're using a TextWriter
(which is what I assume ReportWriter
is): instead it's much more efficient to just write out each concatenated part via .Write
.
Also, I added encloding (and escaped) quotes "
and field-separators: ,
.
So you'll get output like:
Name: "fred", DOB: "1/1/1111", Phone: "1231234567", Address: "1234 lane"
BTW, despite similarity, this is not actually JSON.