Link to home
Start Free TrialLog in
Avatar of ceMo89
ceMo89

asked on

VB.NET textfile + datagridview

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

Avatar of nkewney
nkewney
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of ceMo89
ceMo89

ASKER

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

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

Oh, and of course you'll need to

Imports System
Imports System.IO

Nick
   str = text .Split(",") <--------------- typo :)

Avatar of ceMo89

ASKER

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...
Yep, use this instead.

str = text Split(",") instead

text.Split(",")
Avatar of ceMo89

ASKER

Ok ... ;-)
str = text.Split(",")
thats ok, but i will get theire now an error
Avatar of ceMo89

ASKER

That's my error message....

System.NullReferenceException was unhandled
  Message="Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."
ASKER CERTIFIED SOLUTION
Avatar of nkewney
nkewney
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ceMo89

ASKER

Ok thanks nkewnew,
I've solved the problem.