reading data from dataset bound to a combo box.

H

Hetal

Hi there..

I am a VB6 developer so kinda trying to figure how to work with
VB.NET. I have a scenario where i have a DataSet bound to a combo box,
and i would like to read a row from the DataSet based on the item
selected in the Combo Box.

For e.g.
I have a dataset that has 1 table containing 3 fields say ID, Name,
and Address.
This dataset is bound to a Combo Box with ID as a ValueMember and Name
as a DisplayMember.
The Combo Box fills up perfectly with the Name field.

The problem i am facing is, when i select a Name from the Combo Box, i
would like to read the Address for that Name from the DataSet.

I did find a few samples but they would go through the loop to read
rows in a DataSet, but i could not find a sample where i can read a
row from a DataSet for a selected value.

Any help on this one will be much appreciated. Thanks.

Hetal.
 
S

sloan

If youre workign with winforms (as opposed to asp.net) the number 1 thing to
remember/know about comboboxes.

You put (some kind of) object in a combobox, and NOT value/text pairs.

What do I mean?

If you have a class

public class Emp

and a collection of Emp's
public class EmpCollection

and you bind a combobox to some EmpCollection

Each item in the combobox is an Emp object.

You probably have a DataRow object in each item of the combobox.

You'll have to pull it out, and cast it to work with it.
 
H

Hetal

Thank you sloan. I created an object of class DataRowView and i am
casting the item selected in Combo Box to DataRowView. Keeping the
previous example in mind, here is the snippet of code that i wrote to
get the Address from DataSet.

'In Declaration section.
Dim drvStores As DataRowView

'Code to read the data from dataset based on combo item selection.
Private Sub cmbStores_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbStores.SelectedIndexChanged
drvStores = DirectCast(cmbStores.SelectedItem, DataRowView)
MsgBox(drvStores("Address"))
End Sub

Am i heading the right direction? I mean, is this an efficient way of
reading data from a dataset? Thanks.
 
S

sloan

Youre on the right path.

Private Sub cmbStores_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbStores.SelectedIndexChanged


'try this

dim o as object
o = cmbStores.SelectedItem
if not (o is nothing) then
Console.Write ( o.GetType().ToString() )
'put a watch on "o" in the debugging window, and it should tell you
the type also
end if



' drvStores = DirectCast(cmbStores.SelectedItem, DataRowView)
' MsgBox(drvStores("Address"))





the dim o as object is ONLY FOR FIGURING OUT WHAT YOU GOT.
DO NOT LEAVE THAT IN THERE.

after you have the object type, then you can cast.


PS

You might want to bind your combobox to the dataset.TABLE , not just hte
dataset.

Ex:

if not ( ds.Tables(0) is nothing) then

cbo1.DataSource = ds.Tables(0)

end if

OR better (if its a strong typed dataset)


if not ( ds.Employee is nothing) then

cbo1.DataSource = ds.Employee

end if


where Employee is a strong table name in your dataset.
 

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