[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Populate Datagrid from Array

Posted on 2004-09-28
8
Medium Priority
?
1,188 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

872 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