Solved

Populate Datagrid from Array

Posted on 2004-09-28
8
1,179 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.Net Web Site Password specs 2 32
Spacing between controls 4 29
Passing a form as a parameter to an external class 1 27
update textboxes with Streamreader 21 42
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

14 Experts available now in Live!

Get 1:1 Help Now