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
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)
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?
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
Hi there,
Try using StreamReader like this:
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
Oh, and of course you'll need to
Imports System
Imports System.IO
Nick
Imports System
Imports System.IO
Nick
str = text .Split(",") <--------------- typo :)
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...
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
str = text Split(",") instead
text.Split(",")
ASKER
Ok ... ;-)
str = text.Split(",")
thats ok, but i will get theire now an error
str = text.Split(",")
thats ok, but i will get theire now an error
ASKER
That's my error message....
System.NullReferenceExcept ion was unhandled
Message="Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."
System.NullReferenceExcept
Message="Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok thanks nkewnew,
I've solved the problem.
I've solved the problem.
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