Solved

View a csv file using a datagridview error

Posted on 2008-10-10
3
241 Views
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:

Me.DataGridView1.Rows.Add(SplitLine)
 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, ",")
 

                Me.DataGridView1.Rows.Add(SplitLine)
 

            Loop
 

        Else
 

            MsgBox("File Does Not Exist")
 

        End If

Open in new window

0
Comment
Question by:daruffin
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
ladarling earned 250 total points
Comment Utility
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
0
 

Author Comment

by:daruffin
Comment Utility
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, ",")

                dt.Rows.Add(SplitLine)

                Me.DataGridView1.DataSource = dt.DefaultView

 

            Loop

 

        Else

 

            MsgBox("File Does Not Exist")

 

        End If

0
 

Author Closing Comment

by:daruffin
Comment Utility
I got it to work and readlly thank you for your help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now