Solved

View a csv file using a datagridview error

Posted on 2008-10-10
3
243 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
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
0
 

Author Comment

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

                dt.Rows.Add(SplitLine)

                Me.DataGridView1.DataSource = dt.DefaultView

 

            Loop

 

        Else

 

            MsgBox("File Does Not Exist")

 

        End If

0
 

Author Closing Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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