Solved

Populate Datagrid from Array

Posted on 2004-09-28
8
1,178 Views
Last Modified: 2012-06-27
I have an array that was made via some dynamic SQL that has a variable number of rows and a variable (unknown at design time) number of columns (minimum 2).

I wish to use that array to populate a Datagrid...essentially the process is obtaining data from several tables (unknown which subset of the queried tables will have the data) for a particular name...the one name may be common to more than one person, therefore there may be multiple rows of data.

I want to convert the data from having several LONG rows to several columns (rotating the table).

I have the data into an array (no small feat). now I want to get it into an array.  The examples that I have seen seem to be contingent on a known number of columns...

Can someone help me?
0
Comment
Question by:BodeTech
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Expert Comment

by:wguerram
ID: 12170079
You can to pass the array to a datatable and then assign it to the datagrid.datasource

Is this what you want?

Row1=1523
Row2=5236
Row3=526
Row4=5482

the result would be:

Col1    col2   col3   col4
1523   5236  526   5482

Is this right?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12170093
Hi Bodetech,

Could you post some code so we might get a better idea of what the array looks like and how you are filling it

0
 

Author Comment

by:BodeTech
ID: 12170156
Array(25,2)

Array Contents:
(0,0)fieldName1
(0,1)value
(0,2)value2

(1,0)fieldName2
(1,1)value3
(1,2)value4
...

I want it to display exactly ast the array is laid out

COL1 = Field Name       COL2 = Value 1       COL3 = Value 3
COL1 = Field Name       COL2 = Value 1       COL3 = Value 3
COL1 = Field Name       COL2 = Value 1       COL3 = Value 3
COL1 = Field Name       COL2 = Value 1       COL3 = Value 3
COL1 = Field Name       COL2 = Value 1       COL3 = Value 3
COL1 = Field Name       COL2 = Value 1       COL3 = Value 3
COL1 = Field Name       COL2 = Value 1       COL3 = Value 3
COL1 = Field Name       COL2 = Value 1       COL3 = Value 3
COL1 = Field Name       COL2 = Value 1       COL3 = Value 3


0
 

Author Comment

by:BodeTech
ID: 12170377
as to how I am filling it:

The user inputs the name (Sally for instance)
The app looks up sally in the names table and comes up with one or several "Sally"'s (ID1, ID65, ID934)
The app looks up the various data tables that are available at that time (over time the tables change...this is a huge generalization) (comes up with 5 today)
For each ID (3 of them in this instance)
    For each table (5 in this instance)
        Get values and put them as follows into array
        Row 0 is the Headers (Col 0 is "Field Name", Col1 is "ID1", Col2 is "ID65", Col3 is "ID934")
        Rows 1 and following are the values from the tables for that ID

Essentially, I want to display the data exactly as it is in the array
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 12170753
Hi bodetech,

This is very crude code (I know), I typed just now, but it seems to work

 Dim f(25, 2) As Object
        f(0, 0) = "fieldname1"
        f(0, 1) = 1
        f(0, 0) = 2


        f(0, 0) = "fieldname2"
        f(0, 1) = 3
        f(0, 0) = 4
        Dim dt As New DataTable

        For x As Integer = 0 To f.GetUpperBound(1)
            dt.Columns.Add("col" & x, GetType(Object))
        Next

        For xy As Integer = 0 To f.GetUpperBound(0)
            For yz As Integer = 0 To f.GetUpperBound(1)
                Dim x(f.GetUpperBound(1)) As Object
                For zy As Integer = 0 To f.GetUpperBound(1)
                    x(zy) = f(xy, zy)
                Next
                Dim dr As DataRow = dt.NewRow
                dr.ItemArray = x
                dt.Rows.Add(dr)
            Next
        Next

datagrid1.datasource = dt
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12170813
That's the basic idea i was talking about.

pass the array to a DataTable and the assign it to the DataGrid.DataSource
0
 

Author Comment

by:BodeTech
ID: 12170946
Thanks Ron!

Code I ended up using (though I will probably change variable names a little) was:

                For x As Integer = 0 To finalArray.GetUpperBound(1)
                    dtProfile.Columns.Add(finalArray(0, x))
                Next

                For xy As Integer = 1 To finalArray.GetUpperBound(0)
                    Dim x(finalArray.GetUpperBound(1)) As Object
                    For zy As Integer = 0 To finalArray.GetUpperBound(1)
                        x(zy) = finalArray(xy, zy)
                    Next
                    Dim dr As DataRow = dtProfile.NewRow
                    dr.ItemArray = x
                    dtProfile.Rows.Add(dr)
                Next

With the extra nested loop in there I ended up getting 3 copies of each record.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12171010
Hi Bodetech,

Yeah, sorry about that (I said it was very crude code ;-) )

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now