Query persisted DataSet/DataTable - the easy way?

P

Paul W

I have a combobox bound to a DataView, and I need to get a value from a
persisted DataTable using the ComboBox SelectedIndexChanged event (based on
the value of the item selected). I am successful in doing so, but I may be
doing it the long way around.
Currently, I am creating a DataRow Array and reading the value of a specific
column in the first row. The SelectedIndexChanged event code is listed
below, or you can download the sample project which is a working model using
the same code as below. Download project from
http://www.idhelp.com/ComboBoxTest.zip
If anyone has ideas or suggestions for doing this a better way, please let
me know.
Thanks, Paul
** Code **

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
If bLoadData Then
'data is being bound to object so ignore event
Return
Else
Dim rArray As DataRow() 'array of rows found in datatable
Dim r As DataRow, c As DataColumn

'find the data for the selected record if the selected value is
If ComboBox1.SelectedValue > 0 Then 'dont want data if first
item was selectd
rArray = dsMain.Tables("tblTest").Select("myID = " &
ComboBox1.SelectedValue, "myID", DataViewRowState.CurrentRows)
If rArray.Length = 1 Then 'if not 1 row either there are no
rows or there is more than one row so we don't want any data
For Each r In rArray
For Each c In r.Table.Columns
TextBox1.Text = r("myValueToShow").ToString
Next
Next
Else
MsgBox("The information could not be resolved")
End If

End If
End If
End Sub
 
A

Alex Yakhnin [eMVP]

There are many ways to do that.. How about this one:

DataRow dataRow = dataSet.Tables("tblTest").Rows
(comboBox1.SelectedIndex);

--
Alex Yakhnin, eMVP
IntelliProg, Inc.
http://www.intelliprog.com
"Check out our Compact Framework controls..."
-----Original Message-----

I have a combobox bound to a DataView, and I need to get a value from a
persisted DataTable using the ComboBox
SelectedIndexChanged event (based on
 
P

Paul W

Thanks Alex, that works (and shortens the code).

Alex Yakhnin said:
There are many ways to do that.. How about this one:

DataRow dataRow = dataSet.Tables("tblTest").Rows
(comboBox1.SelectedIndex);

--
Alex Yakhnin, eMVP
IntelliProg, Inc.
http://www.intelliprog.com
"Check out our Compact Framework controls..."

SelectedIndexChanged event (based on
 

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

Top