View a csv file using a datagridview error

Posted on 2008-10-10
Last Modified: 2012-05-05
I have this code and every time I run this code I get a error at this line of Code:

 Error: No row can be added to a DataGridView control that does not have columns. Columns must be added first.

I have attached the csv file I'm trying to view in a datagridview

Dim fName As String = ""
        OpenFileDialog1.InitialDirectory = "c:\temp\"
        OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True
        If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            fName = OpenFileDialog1.FileName
        End If
        Dim TextLine As String = ""
        Dim SplitLine() As String
        If System.IO.File.Exists(fName) = True Then
            Dim objReader As New System.IO.StreamReader(fName)
            Do While objReader.Peek() <> -1
                TextLine = objReader.ReadLine()
                SplitLine = Split(TextLine, ",")
            MsgBox("File Does Not Exist")
        End If

Open in new window

Question by:daruffin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 11

Accepted Solution

ladarling earned 250 total points
ID: 22687908
The datagridview requires columns, it cant infer generic columns from arrays. Instead of trying to directly add the array to the dgv, add it to a datatable first (which can infer columns). Something like:

Dim dt As New DataTable("myTable") 'declare this in classwide scope

dt.Rows.Add(SplitLine) 'Loop over this

DataGridView1.DataSource = dt.AsDataView 'Finish up with this

Author Comment

ID: 22688297
Error Input array is longer than the number of columns in this table

If System.IO.File.Exists(fName) = True Then

            Dim dt As New DataTable("Time")


            Dim objReader As New System.IO.StreamReader(fName)


            Do While objReader.Peek() <> -1


                TextLine = objReader.ReadLine()


                SplitLine = Split(TextLine, ",")


                Me.DataGridView1.DataSource = dt.DefaultView






            MsgBox("File Does Not Exist")


        End If


Author Closing Comment

ID: 31505037
I got it to work and readlly thank you for your help

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question