Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to build a 2-dimensional array (2 columns, varying # of rows)?

Status
Not open for further replies.

Maven4Champ

Technical User
Jun 16, 2004
154
Hi all,

I have a very general question that I hope to find assistance on.

I have a Grid View that is populated with two columns (resp_key and resp_name). I want to loop through this gridview and add the values to a 2-dimensional array (2 columns w/ varying number of rows). From there I want to compare the values in the array to a specific string value. When it finds a match in the string value (resp_key), it display the array item of resp_name.


Data in Grid view is as follows:

resp_key resp_name
ar_key1 Accounts Receivable
ap_key2 Accounts Payable
inv_key3 Inventory

I would like to place those values in an array (dynamically) however I don't always know the number of rows I will have but the # of columns will always and only be 2.

So my psudo-logic is as follows:

Dim 2dimArray As 2-Dimensional Array
For each DataViewRow in DataView
2dimArray.Add(dataviewrow.item(0), datarowview.item(1))
Next

This would essentially provide me with an array as follows:

Print 2dimArray(0, 0) = ar_key1 , Accounts Receivable



Amy advice as I am not very well educated on arrays in VB.NET and how to construct a dynamic multi-dimensional array.

Thanks!
 

How are you populating the DataGridView? If there is a DataTable or DataView for the dataGridView's DataSource, you can do this (assuming the DataSource is a DataTable Named dt):

Dim 2dimArray(1, dt.Rows.Count - 1)) As Object

This will give you a 2-dimensional array with 2 columns and a number of items equal to the number of rows in the DataTable. You can then just loop through the DataTable:

For r As Integer = 0 to dt.Rows.Count - 1
2dimArray(0, r) = dt.Rows(r).Item(0)
2dimArray(1, r) = dt.Rows(r).Item(1)
Next

If for some reason you need to do a dynamic array, here's one way:

Dim 2dimArray(1, 0) As Object

For r As Integer = 0 to dt.Rows.Count - 1
ReDim Preserve 2dimArray(1, r) 'can only redim rightmost element in a multidim. array
2dimArray(0, r) = dt.Rows(r).Item(0)
2dimArray(1, r) = dt.Rows(r).Item(1)
Next



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I am starting with a dataset actually, see code below:

Code:
        Dim oraSQL As String
        oraSQL = "Select column1, column2 from db_table"
        Using connection As New OracleConnection(make_connection(my_environment))
            Dim oracommand As New OracleCommand(oraSQL, connection)
            Dim oraAdapter As New Data.OracleClient.OracleDataAdapter(oracommand)
            oraAdapter.Fill(myDataset, "MY_TABLE")
        End Using
        Dim dv2 As New Data.DataView(myDataset.Tables("MY_TABLE"))

With that, I have Oracle SQL query string, connection, Data Adapter, Fill into Dataset and then reference dataset as a new Data View. How would I go about accomplishing similar results with your syntax?

Keep in mind as you pointed out, I never know the exact # or rows, just that I will never have more than two columns.

Thanks!
 
Ok I tried this and it may work:

Code:
        Dim oraArray(1, myDataset.Tables("MY_TABLE").Rows.Count - 1) As Object
        For r As Integer = 0 To myDataset.Tables("MY_TABLE").Rows.Count - 1
            ReDim Preserve oraArray(1, r)
            oraArray(0, r) = dt.Rows(r).Item(0)
            oraArray(1, r) = dt.Rows(r).Item(1)
        Next

Now I just need to figure out how to use an existing text string and loop through till I find a match in the two-dimensional array (matching on value in column 0 (1st column) and displaying value in column 1 (2nd column).
 

You can loop through an array like this:

For r As Integer = 0 to (2dimArray.Length/2) - 1

Next

You use 2dimArray.Length/2 because the length is the total number of elements in the array. So a 2-dimensional array with 1 "row" has a length of 2, with 2 "rows" a length of 4, etc. Divide the length by 2 to get the number of "rows".


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top