Solved

VB.NET textfile + datagridview

Posted on 2008-10-28
12
522 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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