Solved

VB.NET textfile + datagridview

Posted on 2008-10-28
12
495 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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…

919 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

20 Experts available now in Live!

Get 1:1 Help Now