Solved

Populate Datagrid from Array

Posted on 2004-09-28
8
1,184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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