Results 1 to 12 of 12

Thread: [RESOLVED] Populating a Datafield Grid

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    11

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. reader.SetDelimiters(ControlChars.Tab)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Populating a Datafield Grid

    here's an example. this is the csv:

    one,two,three
    four,five,six
    seven,eight,nine
    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim dt As New DataTable
    5.         dt.Columns.Add("c1")
    6.         dt.Columns.Add("c2")
    7.         dt.Columns.Add("c3")
    8.  
    9.         Dim lines() As String = IO.File.ReadAllLines("csv.txt")
    10.  
    11.         For Each line As String In lines
    12.             Dim dr As DataRow = dt.NewRow
    13.             dr.ItemArray = line.Split(","c)
    14.             dt.Rows.Add(dr)
    15.         Next
    16.  
    17.         DataGridView1.DataSource = dt
    18.  
    19.     End Sub
    20. End Class

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    11

    Post 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?
    Last edited by sylavel; Nov 29th, 2010 at 09:22 PM.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Populating a Datafield Grid

    Quote Originally Posted by sylavel View Post
    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:

    c1 c2 c3
    one two three
    four five six
    seven eight nine
    you could add your datatable columns like this:

    vb Code:
    1. Dim lines() As String = IO.File.ReadAllLines("csv.txt")
    2. Dim dt As New DataTable
    3. dim columnNames() as string = lines(0).split(cchar(vbtab))
    4.  
    5. for each c as string in columnNames
    6.     dt.columns.add(c)
    7. next
    8.  
    9. for x as integer = 1 to lines.getupperbound(0)
    10.     Dim dr As DataRow = dt.NewRow
    11.     dr.ItemArray = lines(x).Split(cchar(vbtab))
    12.     dt.Rows.Add(dr)
    13. next
    14.  
    15. DataGridView1.DataSource = dt

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    11

    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.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Populating a Datafield Grid

    check your delimiters in your text file again

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    11

    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

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Populating a Datafield Grid

    dr.ItemArray = lines(x).Split(cchar(vbTab))

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    11

    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?

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    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:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim lines() As String = IO.File.ReadAllLines("I:\test.txt")
    3.         Dim dt As New DataTable
    4.        
    5.         For x as integer = 1 to lines(0).split(cchar(vbtab)).length
    6.             dt.Columns.Add("Column" & x.tostring)
    7.         Next
    8.         For x As Integer = 0 To lines.GetUpperBound(0)
    9.             Dim dr As DataRow = dt.NewRow
    10.             dr.ItemArray = lines(x).Split(cchar(vbtab))
    11.             dt.Rows.Add(dr)
    12.         Next
    13.                
    14.         DataGridView1.DataSource = dt
    15.  
    16.     End Sub
    17. End Class

  12. #12

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    11

    Re: Populating a Datafield Grid

    exactly what i was hoping for.
    thanks so much paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width