[RESOLVED] Dynamically populate listview from file?
Hello,
I am trying to figure out how to dynamically populate a listview with data from a file, except I only want specific lines and columns from that file. I have code right now that works to populate the listview from the file, but it writes the whole file and I only want specific lines and columns. Here is the code I currently have to populate the listview when the form loads:
VB 2010 Express Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("STFRWSAL.REP")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
currentRow = MyReader.ReadFields()
Dim item As ListViewItem
item = ListView1.Items.Add(currentRow(0))
Dim ix%
For ix = 1 To UBound(currentRow)
item.SubItems.Add(currentRow(ix))
Next
End While
End Using
End Sub
I am new to VB so any suggestions or help would be greatly appreciated.
Re: Dynamically populate listview from file?
can you show us the file? + explain which rows + columns you want
1 Attachment(s)
Re: Dynamically populate listview from file?
I copied the file's data to a .txt file and added it as an attachment becuase the original .REP file can only be opened with certain programs, but anyways I am trying to get the text that starts after the 4 digit number code up until the second identifiers (the B9, Bd, BX, and such) that start at the 34th space. I also want to ignore the fields that have no text directly after the 4 digit number code. I have tried different things with the delimiters, such as spaces and the second indentifiers, but it always either leaves something out or adds too much; plus it always has the number code and I do not want that. Is there maybe a way to set it to write only the text within the 5-33 positions and ignore fields that have no text in these positions? I need to load the list dynamically because the blank fields will not always be blank and the screens I am working on will need to reflect the changes. I know that there is way to do this with COBOL using multi editor because that is how the program I am working on was created originally, but I am supposed to be designing a more modern GUI and would really like to accomplish this with VB. Any help would be great.
Re: Dynamically populate listview from file?
try this:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim items() As ListViewItem = (From line In IO.File.ReadAllLines("workfile[1].txt") _
Where line.Trim <> "" AndAlso line.Substring(4, 29).Trim <> "" _
Select New ListViewItem(line.Substring(4, 29).Trim)).ToArray
ListView1.Items.AddRange(items)
End Sub
End Class
Re: Dynamically populate listview from file?
Thanks a lot Paul. That was exactly what I was looking for and besides it working better than my code it was a lot shorter too. I gave you a good rating. Thanks again.
Re: [RESOLVED] Dynamically populate listview from file?
ok thanks. it's always good to add another rating to my collection:D