Solved

VB.NET textfile + datagridview

Posted on 2008-10-28
12
483 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

19 Experts available now in Live!

Get 1:1 Help Now