473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Just plain reading a CSV into a two dimensional array

Hello - I can read the csv file line by line into a one dimensional array -
no problem. But when the data has 7 columns - and you'd like the Names to
appear in a combo box, it can get tricky.

Fred,0,0,0,0,0,0
Wilma,-1,1,0,0,0,0
Betty,1,0,0,0,-1,0
etc

I found examples elsewhere of using every language BUT VB.NET - so if
somebody wouldn't mind, please? Here is my current status, and even though
I'm only placing the name into a ComboBox now, I plan on making hay with the
rest of the data elsewhere.

Dim intCount as integer
Dim strPeople as string

If System.IO.File.Exists("c:\temp\test.csv") Then
objstreamreader = System.IO.File.OpenText("c:\temp\test.csv")
Do Until objstreamreader.peek = -1
strLine = objstreamreader.readline()
intCount = intCount + 1
Dim strPeople(,) As String = {{strLine}}
Me.RaceComboBox.Items.Add(strPeople(intCount, 1))
Loop
objstreamreader.close()

Nov 21 '05 #1
2 3115
Patrick,

It ain't that straightforward. Add the following code to a form with a
combobox and a textbox. Note a couple of things. I have created a
structure, "FileItem" and also you will want to change the textfile path.
The SelectedIndexChanged event procedure signature has also been modified.
We DON'T want it firing during the loading of the combobox. The AddHandler
at the end of the form_open procedure sets up the delegate...after loading
the combobox.

This example will display the name in the combobox, and the rest of the
string in the textbox, when a name is selected.

HTH,

Larry Woods

(Sorry for everything being left-justified. When you add it to your form it
will reformat.)

CODE:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
TextBox1.Text = CType(ComboBox1.SelectedValue, String)
End Sub

Private Sub frmFillComboFromSeq_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim intCount As Integer
Dim strPeople As String
Dim strLine As String
Dim a As New ArrayList
If System.IO.File.Exists("test.txt") Then
Dim objstreamreader As StreamReader = System.IO.File.OpenText("test.txt")
strLine = objstreamreader.ReadLine
Do Until strLine Is Nothing
intCount = intCount + 1
a.Add(New FileItem(strLine))
Me.ComboBox1.Items.Add(a)
strLine = objstreamreader.ReadLine()
Loop
objstreamreader.Close()
With Me.ComboBox1
..DataSource = a
..DisplayMember = "Name"
..ValueMember = "Detail"
AddHandler .SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
End With
End If
End Sub
End Class
Public Class FileItem
Private targetName As String
Private targetDetail As String
Public Sub New(ByVal fileRecord As String)
MyBase.New()
Dim strTemp As String = Trim(fileRecord)
Dim firstIndex As Integer = strTemp.IndexOf(","c)
targetName = strTemp.Substring(0, firstIndex)
targetDetail = strTemp.Substring(firstIndex + 1)
End Sub
Public ReadOnly Property Detail() As String
Get
Return targetDetail
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return targetName
End Get
End Property
Public Overrides Function ToString() As String
Return targetName & targetDetail
End Function
End Class

END OF CODE

"Patrick" <pa*****@hello.com> wrote in message
news:eg**************@TK2MSFTNGP15.phx.gbl...
Hello - I can read the csv file line by line into a one dimensional array - no problem. But when the data has 7 columns - and you'd like the Names to
appear in a combo box, it can get tricky.

Fred,0,0,0,0,0,0
Wilma,-1,1,0,0,0,0
Betty,1,0,0,0,-1,0
etc

I found examples elsewhere of using every language BUT VB.NET - so if
somebody wouldn't mind, please? Here is my current status, and even though
I'm only placing the name into a ComboBox now, I plan on making hay with the rest of the data elsewhere.

Dim intCount as integer
Dim strPeople as string

If System.IO.File.Exists("c:\temp\test.csv") Then
objstreamreader = System.IO.File.OpenText("c:\temp\test.csv")
Do Until objstreamreader.peek = -1
strLine = objstreamreader.readline()
intCount = intCount + 1
Dim strPeople(,) As String = {{strLine}}
Me.RaceComboBox.Items.Add(strPeople(intCount, 1))
Loop
objstreamreader.close()

Nov 21 '05 #2
Patrick,

It ain't that straightforward. Add the following code to a form with a
combobox and a textbox. Note a couple of things. I have created a
structure, "FileItem" and also you will want to change the textfile path.
The SelectedIndexChanged event procedure signature has also been modified.
We DON'T want it firing during the loading of the combobox. The AddHandler
at the end of the form_open procedure sets up the delegate...after loading
the combobox.

This example will display the name in the combobox, and the rest of the
string in the textbox, when a name is selected.

HTH,

Larry Woods

(Sorry for everything being left-justified. When you add it to your form it
will reformat.)

CODE:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
TextBox1.Text = CType(ComboBox1.SelectedValue, String)
End Sub

Private Sub frmFillComboFromSeq_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim intCount As Integer
Dim strPeople As String
Dim strLine As String
Dim a As New ArrayList
If System.IO.File.Exists("test.txt") Then
Dim objstreamreader As StreamReader = System.IO.File.OpenText("test.txt")
strLine = objstreamreader.ReadLine
Do Until strLine Is Nothing
intCount = intCount + 1
a.Add(New FileItem(strLine))
Me.ComboBox1.Items.Add(a)
strLine = objstreamreader.ReadLine()
Loop
objstreamreader.Close()
With Me.ComboBox1
..DataSource = a
..DisplayMember = "Name"
..ValueMember = "Detail"
AddHandler .SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
End With
End If
End Sub
End Class
Public Class FileItem
Private targetName As String
Private targetDetail As String
Public Sub New(ByVal fileRecord As String)
MyBase.New()
Dim strTemp As String = Trim(fileRecord)
Dim firstIndex As Integer = strTemp.IndexOf(","c)
targetName = strTemp.Substring(0, firstIndex)
targetDetail = strTemp.Substring(firstIndex + 1)
End Sub
Public ReadOnly Property Detail() As String
Get
Return targetDetail
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return targetName
End Get
End Property
Public Overrides Function ToString() As String
Return targetName & targetDetail
End Function
End Class

END OF CODE

"Patrick" <pa*****@hello.com> wrote in message
news:eg**************@TK2MSFTNGP15.phx.gbl...
Hello - I can read the csv file line by line into a one dimensional array - no problem. But when the data has 7 columns - and you'd like the Names to
appear in a combo box, it can get tricky.

Fred,0,0,0,0,0,0
Wilma,-1,1,0,0,0,0
Betty,1,0,0,0,-1,0
etc

I found examples elsewhere of using every language BUT VB.NET - so if
somebody wouldn't mind, please? Here is my current status, and even though
I'm only placing the name into a ComboBox now, I plan on making hay with the rest of the data elsewhere.

Dim intCount as integer
Dim strPeople as string

If System.IO.File.Exists("c:\temp\test.csv") Then
objstreamreader = System.IO.File.OpenText("c:\temp\test.csv")
Do Until objstreamreader.peek = -1
strLine = objstreamreader.readline()
intCount = intCount + 1
Dim strPeople(,) As String = {{strLine}}
Me.RaceComboBox.Items.Add(strPeople(intCount, 1))
Loop
objstreamreader.close()

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
16
by: rguti | last post by:
Hi, How do I create a two dimensional array? I have created a one dimensional doing this: Dim laFields As ArrayList = New ArrayList How about to do a 2 dimensional?
0
by: Patrick | last post by:
Hello - I can read the csv file line by line into a one dimensional array - no problem. But when the data has 7 columns - and you'd like the Names to appear in a combo box, it can get tricky. ...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
5
by: Diffident | last post by:
Hello All, I have a 2-dimensional array that I am storing as a session variable. I have no idea on how I can cast the session variable back to 2-dimensional array. Any pointers? Reference...
3
by: Stuart | last post by:
I am using Visual Basic 2005. I have created a two dimensional ArrayList named aSystem that is populated as follows:- aSystem.Add(New PickList(0, "Undefined")) aSystem.Add(New PickList(-1,...
6
by: fniles | last post by:
I need to store information in a 2 dimensional array. I understand ArrayList only works for a single dimensional array, is that correct ? So, I use the 2 dimensional array like in VB6. I pass the...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
5
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.