By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,581 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,581 IT Pros & Developers. It's quick & easy.

2D array of string

P: n/a
Sam
Hi,
I'm not very good at using arrays in vb. I would like to do the
following :

'browse my dataset's records
For Each dr As DataRow In dsFields.Tables(1).Rows

' add (dr("column_name").ToString to an array of strings[][]
' add (dr("data_type").ToString to the same array of strings[][]
Next

so it should be a 2D array : myArray[column_name][data_type]

How can I do that ?
thx

Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Sam wrote:
'browse my dataset's records
For Each dr As DataRow In dsFields.Tables(1).Rows
' add (dr("column_name").ToString to an array of strings[][]
' add (dr("data_type").ToString to the same array of strings[][]
Next

so it should be a 2D array : myArray[column_name][data_type]


Is this kind of thing what you want:
Dim theArray(dsFields.Tables(1).Rows.Count, 2) As String
Dim rowIndex As Integer

For Each dr As DataRow In dsFields.Tables(1).Rows
theArray(rowIndex, 0) = dr("column_name").ToString
theArray(rowIndex, 1) = dr("data_type").ToString
rowIndex += 1
Next

Then you can access the column names as theArray(rowNum,0) and the datatypes
as theArray(rowNum,1).

--

(O) e n o n e
Nov 21 '05 #2

P: n/a
Sam
thanks it worked. However i can't figure out how to browse through the
records ? Could you give me a loop example ?
thx

Nov 21 '05 #3

P: n/a
Sam,

I certainly would not do that in your combobox chalenge.
When you use something as my sample than you can better create a new table.
That is much easier to set as datatasource and than to use the displaymember
and the value.

Roughly typed in this message

\\\
dim dtnew as new datatable
dim dtnew.column.add("1")
dim dtnew.column.add("2")
for each dr as datarow in my olddatatable.rows
dt.LoadDataRow(New Object() {dr("old1").ToString, dr("old2")ToString}, True)
next
////

I hope this helps,

Cor
Nov 21 '05 #4

P: n/a
Sam wrote:
thanks it worked. However i can't figure out how to browse through the
records ? Could you give me a loop example ?


You mean how to loop through the array and retrieve the data?

Try this:

Dim i As Integer

For i = 0 To UBound(theArray,1)
Debug.WriteLine("Line " & i)
Debug.WriteLine("Column Name = " & theArray(i,0))
Debug.WriteLine("Data Type = " & theArray(i,1))
Next

The UBound function is given a second parameter (the value 1) to indicate
that it should return the number of elements in the first dimension (which
will match the number of rows that were added), rather than the second
dimension (which will always be 2 as there are two columns stored, one for
the Column Name and one for the Data Type).

--

(O) e n o n e
Nov 21 '05 #5

P: n/a
Sam
Oenone,
Thanks. I had done it like this:
For i = 0 To mylist.GetLength(0) - 1
Dim dt() As DataRow = myDatatable.Select("Country = '" &
mylist(i, 0).ToString & "'")
Next
I guess it's the same...

Cor,
I'm not sure what you mean ? Could you explain again if you don't mind
? It is indeed for my combobox challenge ;)

Nov 21 '05 #6

P: n/a
Sam,

I have created a datatable instead of an arraylist.
(The code is completly different from Oenone by the way)

The datatable fits direct on your combobox with a datasource.

The arraylist gives me mostly only one thing when used for that "Trouble".

(I never use that for that forever anymore in Net 1.1).

Cor
Nov 21 '05 #7

P: n/a
Sam
but then in your code of this morning, when you passed a String to
DataGridComboBoxColumn, I should pass a datatable now ?
How can I check an item is selected in another combobox ? I've done it
like this with the array:

For i = 0 To mylist.GetLength(0) - 1
Dim dt() As DataRow = myDatatable.Select("column_name = '"
& mylist(i, 0).ToString & "'")
If dt.Length = 0 Then
ColumnComboBox.Items.Add(mylist(i, 0).ToString)
End If
Next

How can I do it with a datatable ?
thx

Nov 21 '05 #8

P: n/a
Sam,

You said that you said that you did want to use the display and the
valuemember, I typed it in this message so watch typos

However the same with a datatable would be something as absolutly not tested
because you would first have to find how you use can use that display and
value member..

\\\\
dim dtSource as new datatable
dim dtnew.column.add("1")
dim dtnew.column.add("2")
For Each dr As datarow in myCountryTable
Dim drc() As DataRow = myDatatable.Select("Country = '" & dr("Country") &
"'")
If dt.Length = 0 Then
dtSource.LoadDataRow(New Object()
{dr("Country").ToString,dr("CountryValue").ToStrin g}, True)
End If
next
Dim dre() As DataRow = myCountryTable.Select("Country = '" & me.Textbox.Text
& "'")
dtSource.LoadDataRow(New Object()
{dre("Country").ToString,dre("CountryValue").ToStr ing}, True)
///

I hope this helps anyhow

Cor


Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.