[RESOLVED] Populating a Datafield Grid
Hey, I'm trying to bring a text file that is tab delimited so i can preform calculations on it. This is in Visual Studio 2010
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim filename As String = "I:\Test.txt"
Dim dtTest As New DataTable("dtTest")
dtTest.Columns.Add("Col1", GetType(String)) 'this displays the data correctly
Try
Dim reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filename)
reader.TextFieldType = FileIO.FieldType.Delimited
reader.SetDelimiters(" ")
While Not reader.EndOfData
Dim Fields() As String = reader.ReadFields
dtTest.Rows.Add(Fields)
End While
reader.Close()
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
DataGridView1.DataSource = dtTest.DefaultView
End Sub
This code works in populating my data grid but it puts all of columns on my text file into one column. I realize i only added one column within my code but I'm not sure how to make breaks or skips when it reads the text file. I want to have all of column one in column one of the data table and then all of column two of my text file in the datatable. What can I can I change to make this possible so all of my columns and rows aren't in 1 single column of my datagridview?
Re: Populating a Datafield Grid
You're not setting the delimiters properly. If you want to specify a Tab character then use ControlChars.Tab:
vb.net Code:
reader.SetDelimiters(ControlChars.Tab)
Re: Populating a Datafield Grid
here's an example. this is the csv:
Quote:
one,two,three
four,five,six
seven,eight,nine
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("c1")
dt.Columns.Add("c2")
dt.Columns.Add("c3")
Dim lines() As String = IO.File.ReadAllLines("csv.txt")
For Each line As String In lines
Dim dr As DataRow = dt.NewRow
dr.ItemArray = line.Split(","c)
dt.Rows.Add(dr)
Next
DataGridView1.DataSource = dt
End Sub
End Class
Re: Populating a Datafield Grid
thanks for the quick responses!
so..
i changed the delimiter to reader.SetDelimiters(ControlChars.Tab)
and it gives me a input array is longer than the number of columns in this table. So that gives me nothing in my datagridview now. That doesn't help me put the text file data into the same amount of columns in a datagrid view.
Hmm Paul, okay my file is a tab delimited file.
so it would be like
one two three
four five six
seven eight nine
Is there a way to make it take different textfiles and put the respective amount of columns from the text file into the same amount of columns on a datagrid view? such as if i do a 5 column text file or a 7 column text file?
Re: Populating a Datafield Grid
Quote:
Originally Posted by
sylavel
i changed the delimiter to reader.SetDelimiters(ControlChars.Tab)
and it gives me a input array is longer than the number of columns in this table. So that gives me nothing in my datagridview now.
you need to add the appropriate number of columns to your datatable.
/2
my example's only limitation is that you must add the appropriate number of columns to your datatable.
although if for example your csv looked like this:
Quote:
c1 c2 c3
one two three
four five six
seven eight nine
you could add your datatable columns like this:
vb Code:
Dim lines() As String = IO.File.ReadAllLines("csv.txt")
Dim dt As New DataTable
dim columnNames() as string = lines(0).split(cchar(vbtab))
for each c as string in columnNames
dt.columns.add(c)
next
for x as integer = 1 to lines.getupperbound(0)
Dim dr As DataRow = dt.NewRow
dr.ItemArray = lines(x).Split(cchar(vbtab))
dt.Rows.Add(dr)
next
DataGridView1.DataSource = dt
Re: Populating a Datafield Grid
okay great! that works in putting the first value of each column in respective cells.
But with that code it gives me
1 2 3
456
789
with fourfivesix and seveneightnine being in column one row 2.
I need them to be
1 2 3
4 5 6
with 4 5 6 being in columns 1 2 3 restively all in row 2
With this technique will i beable to manipulate these numbers within my code. I wasn't sure if i have to take into account numbers as strictly integers or anything (imagine 1 instead of 1 ect. The text files I'll be using are on a bit of a larger scale. 8 columns around 100 rows.
Re: Populating a Datafield Grid
check your delimiters in your text file again
Re: Populating a Datafield Grid
1 2 3
4 5 6
7 8 9
thats this exact text file i'm using with just a single tab as the delimiter.
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lines() As String = IO.File.ReadAllLines("I:\test.txt")
Dim dt As New DataTable
Dim columnNames() As String = lines(0).Split(vbTab)
For Each c As String In columnNames
dt.Columns.Add(c)
Next
For x As Integer = 1 To lines.GetUpperBound(0)
Dim dr As DataRow = dt.NewRow
dr.ItemArray = lines(x).Split(","c)
dt.Rows.Add(dr)
Next
DataGridView1.DataSource = dt
End Sub
End Class
With the result
1 2 3
456
789
Re: Populating a Datafield Grid
dr.ItemArray = lines(x).Split(cchar(vbTab))
Re: Populating a Datafield Grid
yeah thats perfect and what I was looking for.
The only problem I'm having now with my datagrid view in my first row
1 2 3
is like the heading of it and not individual cells as if they are the column titles. is it possible to make them like individual cells.
4 5 6
are. Is there a way to do that?
Re: Populating a Datafield Grid
i was assuming you'd use a text file with a header row as the first row containing the text you want displayed as your dgv column headers. if you just want standard columnheaders:
vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lines() As String = IO.File.ReadAllLines("I:\test.txt")
Dim dt As New DataTable
For x as integer = 1 to lines(0).split(cchar(vbtab)).length
dt.Columns.Add("Column" & x.tostring)
Next
For x As Integer = 0 To lines.GetUpperBound(0)
Dim dr As DataRow = dt.NewRow
dr.ItemArray = lines(x).Split(cchar(vbtab))
dt.Rows.Add(dr)
Next
DataGridView1.DataSource = dt
End Sub
End Class
Re: Populating a Datafield Grid
exactly what i was hoping for.
thanks so much paul :thumb:
:D:D