Datarelation between two comboboxes?

  • Thread starter Thread starter Tore Bostrup
  • Start date Start date
T

Tore Bostrup

I have searched high and low for documentation I can understand or sample
code for setting up a datarelation between two ComboBoxes. But so far, all
I have found is either not helpful (for instance - "don't post a reply, I
have found out how to do it" but without saying how - which was asked) or
indicates that since the ComboBox doesn't have a DataMember property it
won't work.

I have defined the datarelation and my code runs, but I get all the rows
shown in the "child" combobox.

Can anybody please direct me to a sample or tell me what I am missing
(sorry - my code is on an offline system at the moment but it doesn't work
correctly either...)?

TIA,
Tore.
 
Hello Tore

Can you explain better what exactly are you trying to accomplish with the
two comboboxes ?

Is it that when you select a value in combobox1, you want relevant
information to show in combobox2 ?

If so, you have to make sure the dataSource property of the two comboboxes
are the same, if they are, and you have setup the correct relation in your
dataset, you should be fine.

For some reason if you cannot have the datasource of both these combo boxes
to be the same, you need to use the CurrencyManager Class to work that.

Hope that helps.

HS
 
I want to implement a master/child relationship. When the user selects one
of the values in the first combo, I want to populate the second combo with
the choices related to the first.

Tore.
 
The way i do it is i create a data view for the child table and bind
the view to the combobox. Then handle the SelectedIndexChanged event of
the first combobox and add the following code:

if (comboBox1.SelectedValue is int) {
int val = (int) comboBox1.SelectedValue;
DataView view = (DataView) comboBox2.DataSource
view.RowFilter = "Id = " + val.ToString();
}

comboBox1 is the master selection combobox, where as comboBox2 is the
child combobox. Maybye this method might help you out.
 
The ideal way is to add both these tables in a dataset, set the relationship
between them.

Make the datasource of the child combobox point to the name of the relation
you created and you will have it all working, without writting a single line
of code!

Let me know if you need help.

HS
 
I *do* need help. I'm using code generated datasets, and I'm using the
DisplayMember and ValueMember of the combo boxes. I've set the relation
between them (as best I could figure), but the two boxes just don't
synchronize. All my searching seems to wind up at the CurrencyManager
object, but neither documentation nor examples I've seen help me understand
what I need to do.

Free from memory (I've tried several things, but in essence, my code has
revolved around the following pseudocode statements):

Create and fill "masterTable" in myDataSet
Create and fill "childTable" in myDataSet (with all values)

Create array of DataColumns for masterDS and childDS to use in definint the
relation
Define the relation "relationName" (uses two columns for the relationship,
thus the two arrays)

cboMaster.DataSource = myDataSet.Tables("masterTable")
cboMaster.DisplayMember = "DisplayColumnName"
cboMaster.ValueMember = "ValueColumnName"

cboChild.DataSource = myDataSet.Relations("relationName").ChildTable()
cboChild.DisplayMember = "ChildDisplayColumnName"
cboChild.ValueMember = "ChildValueColumnName"

This is apparently not sufficient to make the two comboboxes synchronized,
but for the life of me I can't figure out what is missing (or wrong).

I've tried several variations on the above, including specifying only the
datatable name instead of the Relations() specification for the child
datasource (the datatables are actually results sets generated by stored
procedures, and not physical database tables), using (or attempting - in
some cases giving errors "couldn't bind to...") different forms of more
fully qualified members ("childTable.Child...ColumnName"), etc.

Sure I can do this with other approaches (I have) - the DataReader doesn't
appear to give me a way to bind a ValueMember - but I tried that last
night/this morning after extensive sleep deprivation, so I don't really
know...

BUT - this is a perfect application for using the DataRelation, so I really
want to learn how to do that (in code).

TIA,
Tore.
 
Yes, I do something similar, but this particular app seems to be a really
good match for use of the DataRelation, and I want to learn how to make it
work.

Thanks anyway,
Tore.
 
Back
Top