Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I download a CSV file from the internet. It consists of 100 or more rows of 20 fields (columns), including the heading row, the first row. I read the file with streamreader convert to an array and load 10 selected columns of the CSV file into a datagridview. I do not use the other CSV columns...they are of no interest. By previewing the CSV file I know the index number of the CSV columns that I want to display in the datagridview, and therefore it is easy to load a particular column from the CSV file into the desired column of the datagridview. Since I have header Titles in the datagridview which don't match exactly with the header names in the CSV file, I read the first line of the CSV file but don't use it, which eliminates the headers from the CSV file from appearing directly below the headers in the datagridview. All of this works fine using the code snippet I show.

The problem is: How to get and use the array index and insert into the code so that I don't have to preview the CSV file. Here is the code I am using.

m objReader As New System.IO.StreamReader(fileName)
            Dim entrantStr As String
            entrantStr = objReader.ReadLine()
            Do While objReader.Peek() <> -1
                entrantStr = objReader.ReadLine()
                entArr = Split(entrantStr, ",")

                'y = Array.IndexOf(entArr, "Rating") <<<<This line of code gets the index but for some reason I cant use it. When I insert y into entArr(y) down below it gives an error message ...out of range.

                dgv1.Rows.Add()
                dgv1(0, i).Value = entArr(1)
                dgv1(1, i).Value = entArr(3)
                dgv1(1, i).Value = Regex.Replace(dgv1(1, i).Value, "[^\d]", "")
                dgv1(2, i).Value = entArr(5)
                dgv1(2, i).Value = StrConv(dgv1(2, i).Value, vbProperCase)
                dgv1(3, i).Value = entArr(15)
                dgv1(4, i).Value = entArr(7)
                dgv1(5, i).Value = entArr(6)
                dgv1(6, i).Value = "--"
                dgv1(7, i).Value = entArr(4)
                dgv1(8, i).Value = "--"
                dgv1(9, i).Value = "--"
                dgv1(10, i).Value = "2019"
                i += 1
            Loop
            objReader.Close()
            objReader.Dispose()


What I have tried:

y = Array.IndexOf(entArr, "Rating")


For Me.y = 0 To entArr.GetUpperBound(0)
                If entArr(y) = "Rating" Then
                End If
      Next
Posted
Updated 1-Dec-18 12:37pm

1 solution

I solved the problem by adding an additional streamreader and reading only the first line. By keeping the first streamreader out of the loop, the out-of-range problem goes away. I then use the array.IndexOf(arrayname, "HeadingText") to get the index, which works as expected. The somewhat cleaned up code is shown below.

Dim msg As New System.IO.StreamReader(fileName)
            Dim myString As String
            myString = msg.ReadLine()
            myArray = Split(myString, ",")
            h0 = Array.IndexOf(myArray, "Division")
            h1 = Array.IndexOf(myArray, "SailNo")
            h2 = Array.IndexOf(myArray, "Boat")
            h3 = Array.IndexOf(myArray, "Class")
            h4 = Array.IndexOf(myArray, "HelmName")
            h5 = Array.IndexOf(myArray, "Club")
            'h6 = Array.IndexOf(myArray, "Buoy")
            h7 = Array.IndexOf(myArray, "Rating")

            Dim objReader As New System.IO.StreamReader(fileName)
            Dim entrantStr As String
            entrantStr = objReader.ReadLine()
            Do While objReader.Peek() <> -1
                entrantStr = objReader.ReadLine()
                entArr = Split(entrantStr, ",")

                dgv1.Rows.Add()
                dgv1(0, i).Value = entArr(h0) '1)
                dgv1(1, i).Value = Regex.Replace(entArr(h1), "[^\d]", "")
                dgv1(2, i).Value = StrConv(entArr(h2), vbProperCase)
                dgv1(3, i).Value = entArr(h3 + 2) '15)
                dgv1(4, i).Value = entArr(h4) '7)
                dgv1(5, i).Value = entArr(h5) '6)
                dgv1(6, i).Value = "--"
                dgv1(7, i).Value = entArr(h7) '4)
                dgv1(8, i).Value = "--"
                dgv1(9, i).Value = "--"
                dgv1(10, i).Value = "2019"
                i += 1
            Loop
            objReader.Close()
            objReader.Dispose()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900