Results 1 to 11 of 11

Thread: [RESOLVED] Reading csv data and storing it to array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Re: Reading csv data and storing it to array

    Thank you for your reply. But I am not sure how to implement your ideas.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Reading csv data and storing it to array

    Well, first things first, what would your type look like to represent a record?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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?

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    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

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    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.

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    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
  •  



Click Here to Expand Forum to Full Width