[RESOLVED] Dinamically fill datatable rows
Hello,
I have an array numbers that contanis some data that need to display on datagrid so I'm buildind a datatable. It also contains the headers, and the numbers of rows and columns.
Of course this parameters can change so as the columns and rows.
The goal is sommething like this:
|100 | 200 | 300 | 400 | 500 |
-----------------------------------
20 | 0 | 18 | 23 | 65 | 41 |
30 | 22 | 91 | 64 | 33 | 18 |
40 | 44 | 63 | 91 | 26 | 32 |
50 | 61 | 83 | 91 | 26 | 32 |
where the bolded numbers are the headers.
in the code I'm exctacting the headers, creating the columns at runtime, but I'm stuck in how create and fill the rows.
Any advice?
Code:
Private Function getdata() As DataTable
Dim numbers = New Integer() {61, 61, 61, 61, 61, 20, 30, 40, 50, 100, 200, 300, 400, 500, 0, 0, 0, 0, 0, 18, 23, 65, 41, 22, 91, 64, 33, 18, 44, 63, 91, 26, 32, 61, 83, 91, 26, 32, 91, 91, 91, 91}
Dim nC As Integer = 5
Dim nR As Integer = 4
Dim K As Double = 0.5
Dim StartbpC As Integer = 18
Dim StartbpR As Integer = 5
Dim bpc As New List(Of Integer)
For I = 0 To nC - 1
bpc.Add(numbers(StartbpC + I))
Next
For I = 0 To nR - 1
bpc.Add(numbers(StartbpR + I))
Next
Dim dt As New DataTable("map")
Dim column As DataColumn
Dim row As DataRow
column = New DataColumn
column.DataType = System.Type.GetType("System.Int32")
For I = 0 To bpc.Count - 1
column = New DataColumn
column.DataType = System.Type.GetType("System.Int32")
column.ColumnName = bpc(I).ToString
column.Caption = bpc(I).ToString
dt.Columns.Add(column)
Next
Dim dataSet As New DataSet()
dataSet.Tables.Add(dt)
For I = 0 To nR - +1
Dim myrow = dt.NewRow()
'I'm stuck here
dt.Rows.Add()
Next
Return dt
End Function
Re: Dinamically fill datatable rows
There's no point in adding a dataset. Those are only if you have a collection of datatables. You have one datatable, so the dataset does nothing for you.
You can probably set this up a bit more simply:
Code:
For I = 0 To bpc.Count - 1
dt.Columns.Add(bpc(I).ToString,GetType(Integer))
Next
The caption will be the column name, which is the first argument to that version of Columns.Add. The GetType(Integer) is just a shorter version of what you are doing for the datatype. Either one will work.
Set up the datatable first, including adding the columns. The rows come later.
Once you have the datatable set up, you can do something like this:
Code:
Dim newRow = dt.NewRow
dt.Rows.Add(newRow)
For I = 0 To bpc.Count - 1
newRow(bpc(I).ToString) = whatever
Next
This probably won't set up your row headers quite right, but it will fill the table and give you the proper column headers.
Re: Dinamically fill datatable rows
Ok, thank you.
I'm missing one thing:
since the number of column will change( nc is a variable), how can I set up the rows at runtime?
I mean this line:
Code:
newRow(bpc(I).ToString) = whatever
Re: Dinamically fill datatable rows
That's why it's in a loop. What that is saying is "Put whatever into column bpc(I) of the new row." All that will do is fill a single row, but it will fill 'I' fields in that row.
Re: Dinamically fill datatable rows
Quote:
Originally Posted by
Shaggy Hiker
That's why it's in a loop. What that is saying is "Put whatever into column bpc(I) of the new row." All that will do is fill a single row, but it will fill 'I' fields in that row.
I'm sorry, I'm not sure that I have understand in the right way.
this loop is executed for each columns 1 time to fill a row, so I have to put it into another loop to add nC rows, right?
"whatever" contain one field at each step right, right?
Re: Dinamically fill datatable rows
I get it It work. What about rows header, I didn't found much on microsoft documentation to set them as customm value.
Re: [RESOLVED] Dinamically fill datatable rows
I have only vague memories of trying to mess with row headers in DGVs. I don't know what I ended up doing.