Solved

VB.NET textfile + datagridview

Posted on 2008-10-28
12
517 Views
Last Modified: 2008-10-28
Hey guys,
I've a question about the datagridview....
I would like to import a textfile(e.g: file1.txt)  to my datagridview1 ...

File1.txt looks like this:
1, peter, peterson, street 12

My datagridview has 4 columns
ID, Name, Firstname, Address

Below I've posted two code snippets.
Codesnippet1 will works fine, but it doesn't read something from a text file...so I've write a new code...(codesnippet2) ...
This code read the textfile and will save it to a string...called str
But codesnippet 2 fill just the first column with all the datas....

I hope you will be able to understand my question, because my english isn't really nice....

Thanks


'Codesnippet 1
 
        Dim str As String() = {"1", "peter", "peterson", "street 12"}
        DataGridView1.Rows.Add(str)
 
'Codesnippet 2
        Dim fileText As String = My.Computer.FileSystem.ReadAllText("C:\Temp\File1.txt")
 
        Dim str As String() = {fileText}
        DataGridView1.Rows.Add(str)

Open in new window

0
Comment
Question by:ceMo89
[X]
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
  • 7
  • 5
12 Comments
 
LVL 1

Expert Comment

by:nkewney
ID: 22821652
Hi there,

Try using the split function the fileText string

Assuming your file only has one line, use this:

Dim str As String() = fileText.Split(",")
DataGridView1.Rows.Add(str)

Nick
0
 

Author Comment

by:ceMo89
ID: 22821870
Hey nkewney,
thanks for your answer.
It works.
But how is it possible to import File1.txt if File1.txt looks like this:

1, Peter, Peterson, Street1
2, Jurg, Bom, Roflsne 12

I've think it can be done like my code below....but it doesn't work
Any ideas?



        For Each line In fileText
            Dim str As String() = fileText.Split(",")
            DataGridView1.Rows.Add(str)
        Next

Open in new window

0
 
LVL 1

Expert Comment

by:nkewney
ID: 22821946
Hi there,

Try using StreamReader like this:
Dim SourceFile As FileInfo = New FileInfo("C:\Temp\File1.txt") 
Dim reader As StreamReader = SourceFile.OpenText() 
 
Dim str as String()
Dim text As String 
 
Do 
    text = reader.ReadLine() 
    str = fileText.Split(",")
    DataGridView1.Rows.Add(str)
 
Loop While text IsNot Nothing 

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:nkewney
ID: 22821972
Oh, and of course you'll need to

Imports System
Imports System.IO

Nick
0
 
LVL 1

Expert Comment

by:nkewney
ID: 22822031
   str = text .Split(",") <--------------- typo :)

0
 

Author Comment

by:ceMo89
ID: 22822050
Hey,

Imports System
Imports System.IO

are already imported.
But in your new code snippet you didn't declare "filetext".
Mhh it doesn't work....i don't have any else ideas...
0
 
LVL 1

Expert Comment

by:nkewney
ID: 22822064
Yep, use this instead.

str = text Split(",") instead

0
 
LVL 1

Expert Comment

by:nkewney
ID: 22822067
text.Split(",")
0
 

Author Comment

by:ceMo89
ID: 22822077
Ok ... ;-)
str = text.Split(",")
thats ok, but i will get theire now an error
0
 

Author Comment

by:ceMo89
ID: 22822150
That's my error message....

System.NullReferenceException was unhandled
  Message="Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."
0
 
LVL 1

Accepted Solution

by:
nkewney earned 250 total points
ID: 22822219
Try adding a try...catch to handle blank lines so you can isolate the problem.

Nick


 Dim SourceFile As FileInfo = New FileInfo("C:\Temp\File1.txt")
        Dim reader As StreamReader = SourceFile.OpenText()
 
        Dim str As String()
        Dim text As String
 
        Do
            Try
                text = reader.ReadLine()
                str = text.Split(",")
                DataGridView1.Rows.Add(str)
            Catch ex As Exception
                text = Nothing
            End Try
        Loop While text IsNot Nothing

Open in new window

0
 

Author Comment

by:ceMo89
ID: 22822287
Ok thanks nkewnew,
I've solved the problem.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…

726 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