PC Review


Reply
Thread Tools Rate Thread

2D array of string

 
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
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

 
Reply With Quote
 
 
 
 
Oenone
Guest
Posts: n/a
 
      5th Apr 2005
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


 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
thanks it worked. However i can't figure out how to browse through the
records ? Could you give me a loop example ?
thx

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      5th Apr 2005
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


 
Reply With Quote
 
Oenone
Guest
Posts: n/a
 
      5th Apr 2005
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


 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
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

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      5th Apr 2005
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


 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      5th Apr 2005
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

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      5th Apr 2005
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




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)' to '1-dimensional array of String'. roidy Microsoft VB .NET 12 17th Jul 2009 10:53 AM
Problems Loading Large String Array into Array variable ExcelMonkey Microsoft Excel Programming 6 6th May 2009 11:20 PM
Is there a performance difference between TextWriter.WriteLine(String)and TextWriteLine(String, array<Object>[])? Author Microsoft C# .NET 3 23rd Jun 2008 07:34 PM
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= Microsoft ASP .NET 0 4th May 2006 05:29 PM
Cannot create an object of type 'System.String[]' from its string representation 'String[] Array' for the 'Options' property. Hessam Microsoft C# .NET 0 8th Aug 2003 09:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:22 PM.