Write Row by Row of a unbound DatagridView to a textfile. HELP!!

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I need to write data that is contained in a datagridview to a text file row by row.
I cant get this to work.
<pre> Dim row1 As String
Dim cell1 As String
Dim cell2 As String
Dim cell3 As String
Dim cell4 As String
Dim cell5 As String
Dim cell6 As String
Dim cell7 As String
Dim cell8 As String
Dim cell9 As String
Dim cell10 As String





Do Until a = (DataGridView1.Rows.Count.ToString)
Dim row As Integer

cell1 = (DataGridView1.Rows(row).Cells(0).Value.ToString)
cell2 = (DataGridView1.Rows(row).Cells(1).Value.ToString)
cell3 = (DataGridView1.Rows(row).Cells(2).Value.ToString)
cell4 = (DataGridView1.Rows(row).Cells(3).Value.ToString)
cell5 = (DataGridView1.Rows(row).Cells(4).Value.ToString)
cell6 = (DataGridView1.Rows(row).Cells(5).Value.ToString)
cell7 = (DataGridView1.Rows(row).Cells(6).Value.ToString)
cell8 = (DataGridView1.Rows(row).Cells(7).Value.ToString)
cell9 = (DataGridView1.Rows(row).Cells(8).Value.ToString)
cell10 = (DataGridView1.Rows(row).Cells(9).Value.ToString)



cell1 = cell1.PadRight(7)
cell2 = cell2.PadRight(3)
cell3 = cell3.PadRight(7)
cell4 = cell4.PadRight(2)
cell5 = cell5.PadRight(4)
cell6 = cell6.PadRight(3)
cell7 = cell7.PadRight(12)
cell8 = cell8.PadLeft(9)
cell9 = cell9.PadLeft(9)
cell10 = cell10.PadLeft(9)

row1 = cell1 & cell2 & cell3 & cell4 & cell5 & cell6 & cell7 & cell8 & cell9 & cell10

Dim path As String = "recordsNEW.txt"

Dim sw As StreamWriter

If File.Exists(path) = False Then
sw = File.CreateText(path)

sw.WriteLine("JOB NUMBER SECTION")

sw.Flush()
sw.Close()
End If

sw = File.AppendText(path)

sw.WriteLine(row1)

sw.Flush()
sw.Close()
a += 1
Loop[/code]
or am i going about this the wrong way. Just cant think logically today.
thanks in advance.

View the full article
 
Back
Top