Results 1 to 6 of 6

Thread: Fill a Data Table

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    11

    Fill a Data Table

    Hello,

    I need to populate a DataTable in order to display it with a DataGridView afterwards.

    I stored in a variable a string of 670 characters that I have to split into groups of 10 in order to display them in the table, filling it row by row.

    So I try to fill the table box by box by going through the number of columns and as soon as we arrive at 10 (the number of columns), we go to the next line, continuing to fill it out from the string which is cut out.

    I'm wondering if he best way is it to make a list of string which contains 67 string of 10 characters (the starting string which is split), to create the number of lines starting from the list and to try to fill the array with from that?

    Name:  Capture.jpg
Views: 799
Size:  15.4 KB


    Dim table As New DataTable()
    table.Columns.Add("10", Type.GetType("System.String"))
    table.Columns.Add("20", Type.GetType("System.String"))
    table.Columns.Add("30", Type.GetType("System.String"))
    table.Columns.Add("40", Type.GetType("System.String"))
    table.Columns.Add("50", Type.GetType("System.String"))
    table.Columns.Add("60", Type.GetType("System.String"))
    table.Columns.Add("70", Type.GetType("System.String"))
    table.Columns.Add("80", Type.GetType("System.String"))
    table.Columns.Add("90", Type.GetType("System.String"))
    table.Columns.Add("100", Type.GetType("System.String"))


    Dim testStr As New List(Of String)
    testStr.Add("test1")
    testStr.Add("test2")
    testStr.Add("test3")
    testStr.Add("test4")
    testStr.Add("test5")
    testStr.Add("test6")
    testStr.Add("test7")
    testStr.Add("test8")
    testStr.Add("test9")
    testStr.Add("test10")
    testStr.Add("test11")
    testStr.Add("test12")


    For k = 0 To table.Columns.Count - 1
    Dim putNewRow As DataRow = table.NewRow
    putNewRow(k) = testStr(k).ToString
    table.Rows.Add(putNewRow)


    Next



    So far I have triedwith the code above, just to try to fill the array as desired but I couldn't.
    The goal would be that from "test1" to "test10", we insert on the first line, from "test11" to "test20" on the next, etc ...

    Thanks in advance for the help
    Last edited by eratail

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

    Re: Fill a Data Table

    First things first, this:
    vb.net Code:
    1. table.Columns.Add("10", Type.GetType("System.String"))
    would be better done like this:
    vb.net Code:
    1. table.Columns.Add("10", GetType(String))

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    11

    Re: Fill a Data Table

    I corrected it, thank you

    Do you have an idea how to solve it ?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Fill a Data Table

    To be clear, you have a String with 670 characters and you want to populate the grid with 10 columns and 67 rows with one character in each cell, correct?

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    11

    Re: Fill a Data Table

    The size of the string can change, so i'll always divide the string by 10 to makes group of 10 caracters, and i want to put 10 caracters of this string in each cell, i don't know if i'm clear enough ? Otherwise i'll try to explain as much as i can.


    Something like this :

    Name:  Capture2.PNG
Views: 650
Size:  53.8 KB
    Last edited by erataille; Apr 30th, 2021 at 06:43 AM.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Fill a Data Table

    Have a look at the LINQ .Skip and .Take methods, to see how you can take a subset of 10 values from your array

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