2D array of string

  • Thread starter Thread starter Sam
  • Start date Start date
S

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
 
Sam said:
'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).
 
thanks it worked. However i can't figure out how to browse through the
records ? Could you give me a loop example ?
thx
 
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
 
Sam said:
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).
 
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 ;)
 
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
 
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
 
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").ToString}, True)
End If
next
Dim dre() As DataRow = myCountryTable.Select("Country = '" & me.Textbox.Text
& "'")
dtSource.LoadDataRow(New Object()
{dre("Country").ToString,dre("CountryValue").ToString}, True)
///

I hope this helps anyhow

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top