?
Solved

Populate Datagrid from Array

Posted on 2004-09-28
8
Medium Priority
?
1,186 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
Technology Partners: 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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