-
Mar 5th, 2014, 03:14 AM
#1
Thread Starter
Junior Member
[RESOLVED] Reading csv data and storing it to array
HI,
I was able to read the csv data and display the data in the Datagridview but I didn't know how to save those data in array form. My code looks like following:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
OpenFileDialog1.ShowDialog()
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(OpenFileDialog1.FileName)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Application.DoEvents()
Try
currentRow = MyReader.ReadFields()
With DataGridView1
.ColumnCount = 2
Dim row As String() = New String() {currentRow(0), currentRow(1)}
.Rows.Add(row)
End With
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
Catch ex As Exception
End Try
End Sub
If anyone could give hints on how to save data on arrarys that would be great because I want to plot graphs from the loaded csv data.
Thanks.
-
Mar 5th, 2014, 03:27 AM
#2
Re: Reading csv data and storing it to array
If you're going to read the file line by line then you can't really use an array, at least to begin with. You would use a collection instead, which will grow dynamically as you Add items.
You should start by defining a type that can store a single record. If your record contains two String values then your class might look something like this:
Code:
Public Class Thing
Public Property Value1 As String
Public Property Value2 As String
End Class
If the number and or type of the fields is different then vary your definition accordingly. Use more appropriate names as well.
You can then create a List(Of Thing) and use that to store your records. As you read a record, you create an instance, populate its properties and then Add it to the collection. When you're done reading, your collection can be used just like an array for the most part. If you specifically need an array then you can call ToArray on the collection to get one.
-
Mar 5th, 2014, 04:04 AM
#3
Thread Starter
Junior Member
Re: Reading csv data and storing it to array
Thank you for your reply. But I am not sure how to implement your ideas.
-
Mar 5th, 2014, 04:28 AM
#4
Re: Reading csv data and storing it to array
Well, first things first, what would your type look like to represent a record?
-
Mar 5th, 2014, 11:16 AM
#5
Thread Starter
Junior Member
Re: Reading csv data and storing it to array
My simple dataset looks like following:
Code:
date, distance
1,5
2,8
4,9
10,15
Thanks.
-
Mar 5th, 2014, 11:26 AM
#6
Re: Reading csv data and storing it to array
That's not what I asked. I said that you should start by defining a type to represent a single record and I gave you an example of what that might look like. What should that type look like for your data? What should the name of the class be? How many properties should it have? What should be the types of the properties? What should be the names of those properties?
-
Mar 5th, 2014, 11:32 AM
#7
Thread Starter
Junior Member
Re: Reading csv data and storing it to array
Are you asking for this ?
It would look something like:
Code:
Public Class flow
Public Property point As double
Public Property distance As double
End Class
-
Mar 5th, 2014, 11:54 AM
#8
Re: Reading csv data and storing it to array
wait... CurrentRow IS an array... so why are you doing this:
Dim row As String() = New String() {currentRow(0), currentRow(1)}
You're taking an array, and sending each element to another array...
I don't understand what the problem is... ReadFeilds of the text file parser returns a string array...
So what's the problem?
jsut do this:
Code:
currentRow = MyReader.ReadFields()
With DataGridView1
.Rows.Add(currentRow)
End With
You might even be able to do this:
Code:
With DataGridView1
.Rows.Add(MyReader.ReadFields())
End With
now, if you need them to be doubles and not strings, then try this:
Code:
currentRow = MyReader.ReadFields()
With DataGridView1
.Rows.Add(new Double() {cdbl(currentRow(0)), cdbl(currentRow(1))})
End With
Be sure to skip incomplete rows, and the header too...
-tg
-
Mar 5th, 2014, 12:21 PM
#9
Thread Starter
Junior Member
Re: Reading csv data and storing it to array
The problem here is not with reading the data into datagridview1.
But I want to either use datagridview1 data to plot charts or directly read csv file in an array so that I can plot graphs.
For example to plot chart, I have something like this :
Code:
Dim xdat As Array
Dim ydat As Array
xdat = {0, 1, 2, 3, 4, 5}
ydat = {5.5, 10.1, 11.3, 14, 15}
chart.Show()
chart.Chart1.Series(0).Points.DataBindXY(xdat, ydat)
chart.Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
How can i replace xdat, ydat with the data in the Datagridview or data from csv ?
As mentioned earlier csv looks like following:
Code:
date, distance
1,5
2,8
4,9
10,15
I am coming from R programming background and it is so trivial to do such thing there but I am so stuck in this simple thing. I need to build an application so I am using vb.net.
Thanks.
-
Mar 5th, 2014, 01:15 PM
#10
Re: Reading csv data and storing it to array
Code:
'Outside the loop
Dim xLDat as new List(of Decimal)
dim yLDat as new List(Of Decimal)
'inside the loop while reading...
currentRow = MyReader.ReadFields()
xLDat.Add(cdec(currentRow(0)))
yLDat.Add(cdec(currentRow(1)))
'After the loop and done reading...
xDat = xLDat.ToArray
yDat = yLDat.ToArray
'Done
-tg
-
Mar 6th, 2014, 02:01 AM
#11
Thread Starter
Junior Member
Re: Reading csv data and storing it to array
Techgnome,
Thank you so much. It works great.
Tags for this Thread
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
|