CSV -> DataGridView

Ryan1

Member
Joined
Aug 18, 2013
Messages
8
Programming Experience
Beginner
I am currently trying to load a CSV file into a data table. Here is the code I am using to do so:

Imports System.IO
 
Public Class Form1
 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 
    End Sub
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim SR As StreamReader = New StreamReader("C:\Tester.csv")
        Dim i As Long = 0
        Dim line As String = SR.ReadLine()
        Dim strArray As String() = line.Split(",")
        Dim dt As DataTable = New DataTable()
        Dim row As DataRow
 
        For Each s As String In strArray
            dt.Columns.Add(New DataColumn())
        Next
        Do
            line = SR.ReadLine
            row = dt.NewRow()
            row.ItemArray = line.Split(",")
            dt.Rows.Add(row)
 
        Loop While Not line = String.Empty
 
        DataGridView1.DataSource = dt
 
 
    End Sub
End Class



I am getting a:

Object reference not set to an instance of an object

Regarding the:

row.ItemArray = line.Split(",")


I am new to VB. Trying to build a program that can go through and error check the values of massive CSV files. I will eventually be throwing in IF statements. Any chance I can get this running? Thanks
 
Last edited by a moderator:
Back
Top