Как получить индекс массива и использовать его для загрузки datagridview
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.
Проблема заключается в следующем: Как получить и использовать индекс массива и вставить его в код, чтобы мне не пришлось просматривать CSV-файл. Вот код, который я использую.
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()
Что я уже пробовал:
y = Array.IndexOf(entArr, "Rating")
For Me.y = 0 To entArr.GetUpperBound(0) If entArr(y) = "Rating" Then End If Next