?
Solved

VB.NET textfile + datagridview

Posted on 2008-10-28
12
Medium Priority
?
526 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 1000 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

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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 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