-
April 18th, 2014, 10:34 AM
#1
Load CSV to Listview including Subitems
Hello all!
Using Visual Basic 2008, I Want to load a csv file to Listview.
I am new to LINQ, and had some great performance increase in using LINQ (compared to loops) when loading a csv file to a single column Listview.
Example Code:
Code:
Dim items() As ListViewItem = (From line In IO.File.ReadAllLines(thefiletoread) Where line.Length <> 0 Select New ListViewItem(RTrim(line.ToUpper))).ToArray
ListView2.Items.AddRange(items)
altought, i have not found any way to do the exact same thing, but in a multiple column Listview.
I do not wish to use loops, since LINQ syntax works so well.
any of you guys have an idea?
-
May 6th, 2014, 10:31 AM
#2
Re: Load CSV to Listview including Subitems
I see you mention performance, but I don't really see how LINQ could be quicker in this case. A loop will almost always give significantly better performance when compared to a LINQ method.
If you can provide a file layout and the target listview column setup, I'm sure someone can give you a quick example of how to do this.
Good Luck,
Craig - CRG IT Solutions - Microsoft Gold Partner
-My posts after 08/2015 = .NET 4.x and Visual Studio 2015
-My posts after 11/2011 = .NET 4.x and Visual Studio 2012
-My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
-My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
-My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
-My posts before 04/2007 = .NET 1.1/2.0
*I do not follow all threads, so if you have a secondary question, message me.
-
May 7th, 2014, 04:39 AM
#3
Re: Load CSV to Listview including Subitems
Hello!
Thanks for your answer....
The input text file is a TAB separated file. I have no idea upon import how much columns is needed in the Listview.
By Default my List view has a single column. If i detect the presence of one or more TAB character, i programmatically add the column to add the data split from the original input String.
So let's say my input file is: (note replaced TABs by Spaces since the website inputbox does not allow for TABs)
Banana Fruit Yellow
Apple Fuit Red
Cocumber Vegetable Green
Note1: I do not know the quantity of TAB separator Characters (if any), but all lines contain the same number of column. (from 0 to 83 approx)
Note2: My file can contain up to 100 000 lines
Currently, my code is:
Code:
Dim items() As ListViewItem = (From line In IO.File.ReadAllLines(thefiletoread) Where line.Length <> 0 Select New ListViewItem(RTrim(line.ToUpper))).ToArray
ListView2.Items.AddRange(items)
This is quick, but has no column Logic...
So i would need to
-Import the file Line by line
-Scan the first line as string and determine quantity of TAB separator Characters
-Add the Missing columns to Listview
-Split the Input Line String
-Loop trough all Portions of the Splitted string to add to listview Item variable (not adding to Listview for now)
-Continue looping trough all line of input document
-Detect End if File
-Add listview Item Variable to Listview with addrange
Do you agree?
I really have the feeling that importing using Linq is quicker than loops... but i may be wrong.
I will have to test this new logic and compare the time it takes.
My past experience of importing big text files could take up to 5 minutes (with no multi-column analysis), when similar Ling import would take less than 30 sec.
Here is base code from other web site: (http://bytes.com/topic/visual-basic-...view-add-items)
Code:
Dim i As ListViewItem = ListView1.Items.Add("First portion from file")
i.SubItems.Add("Second portion from file")
i.SubItems.Add("third portion from file")
-
May 20th, 2014, 07:34 AM
#4
Re: Load CSV to Listview including Subitems
Here is a quick example that should give some insight:
Code:
Private Sub LoadDataToListview()
Dim filePath As String = "C:\myfile.txt"
Dim streamReader As New IO.StreamReader(filePath)
Dim streamText As String
Dim listViewItem As ListViewItem
While Not streamReader.EndOfStream
streamText = streamReader.ReadLine()
If Not String.IsNullOrEmpty(streamText) Then
If Not ListView1.Columns.Count = 0 Then
'
'Add listview items
'
listViewItem = New ListViewItem(streamText.Split(ControlChars.Tab))
'
'Do additional work on values here
'
ListView1.Items.Add(listViewItem)
Else
'
'Populate Column Headers
'
For Each value As String In streamText.Split(ControlChars.Tab)
ListView1.Columns.Add(value)
Next
End If
End If
End While
streamReader.Dispose()
End Sub
Good Luck,
Craig - CRG IT Solutions - Microsoft Gold Partner
-My posts after 08/2015 = .NET 4.x and Visual Studio 2015
-My posts after 11/2011 = .NET 4.x and Visual Studio 2012
-My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
-My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
-My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
-My posts before 04/2007 = .NET 1.1/2.0
*I do not follow all threads, so if you have a secondary question, message me.
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
|