Reputation: 37
I'm stumped, I cannot seem to figure out why this loop only produces the following in the datagridview:
The code I am using is:
Private Sub ReadTextFiles()
Dim textline As String
Dim dt As New DataTable
dt.Columns.Add("Date & Time")
dt.Columns.Add("Name")
dt.Columns.Add("tpnb")
dt.Columns.Add("Description")
dt.Columns.Add("id")
dt.Columns.Add("Department")
dt.Columns.Add("Price")
Dim fileentries As String() = Directory.GetFiles("D:\Comp Sci\ASC_BCKUP_260119\ASC_w-Login\bin\Debug\Orders")
For Each entry In fileentries
Dim linecount As Integer = File.ReadAllLines(entry).Length
Dim rowcount As Integer = linecount / 7
Using reader As New StreamReader(entry)
For rowcount = 0 To rowcount - 1
dt.Rows.Add()
For col = 0 To 6
textline = reader.ReadLine()
TextBox1.Text += textline
dt.Rows(rowcount)(col) = textline
Next
Next
End Using
Next
DataGridView1.DataSource = dt
'DataGridView1.Columns("Price").DefaultCellStyle.Format = "£00:00"
End Sub
I've added textbox1
to see what it contains and it contains all of the data, however the datagridview doesn't and I can't see why. The datagridview only appends 3 product's information to it, why I'm not sure why it does.
The directory contains 3 text files with 35 lines in total, so a total of 5 products should be visible in the datagridview.
To clear things up: The loop here should be iterating over the datagridview and writing the relevant information into each cell. For each product there is 7 bits of information, as can be seen in the image of the datagridview.
I have tried using dim dr = dt.NewRow()
and dt.rows.add(dr)
however the datagridview is now blank, containing only the column headers.
Here is an image of one of the text files.
Any help is greatly appreciated to relieve me from the frustration.
Upvotes: 0
Views: 140
Reputation: 15091
This should work for any number of files with any number of products in each file as long as the number of lines per products remains at 7.
Private Sub ReadTextFiles()
Dim dt As New DataTable
dt.Columns.Add("Date & Time")
dt.Columns.Add("Name")
dt.Columns.Add("tpnb")
dt.Columns.Add("Description")
dt.Columns.Add("id")
dt.Columns.Add("Department")
dt.Columns.Add("Price")
'3 files
'7 fields
'35 lines total
'5 products
Dim fileentries As String() = Directory.GetFiles("TestFiles")
Dim lstLines As New List(Of String)
For Each entry In fileentries
lstLines.AddRange(File.ReadAllLines(entry))
Next
'Now we have all the data lines in a list
Dim i As Integer
Dim OuterLoopIterations As Integer = CInt(lstLines.Count / 7)
For iterations = 0 To OuterLoopIterations - 1
Dim row = dt.NewRow
For col = 0 To 6
row(col) = lstLines(i)
i += 1
Next
dt.Rows.Add(row)
Next
DataGridView1.DataSource = dt
End Sub
Upvotes: 1
Reputation: 163
From just looking at your code - Your dt.Rows.Add() is what adds rows to your datatable. It is only adding the 3 rows available at the point you call it. Put a break point above it and see whats available when its adding to the datatable. You might have to move that statement. Also, you can check which file its adding from - the 3 items displayed are they from different files or the same? That will tell you at what point your adding is faulty.
Upvotes: 0