473,386 Members | 1,835 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,386 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 3114
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.