combobox n dataset

  • Thread starter Thread starter Dino L.
  • Start date Start date
D

Dino L.

Hi,

I have a problem, and I just cant fint a good way to solve it.
I have 3 tables in my DB
Table1
-------
IDTable1
....

Table2
-------
IDTable2
IDTable1 (forign key -> Table1)
....

Table3
-------
IDTable3
IDTable2 (forign key -> Table2)
....

Now, on form load I need to fill one combo box with data from table1.
That works good, I just create dataset, fill it with tables, and fill
combo with IDTable1.

But now,
When someone choose one IDTable1 from first combo I have to fill second
combo with data from Table2 (according to forign key
Table2.IDTable1=Table1.IDTable1)??? Also when he choose another IDTable1
from first combo, datas in second combo should also change. (same way is
for 3th combo).

Which event on combo should I use, how to fill second combo after that
event ?!?
 
Dino,

You actually don't have to do anything in order to handle this scenario.
What you want to do is load the data from the three tables into one data set
(three separate tables). Once you do that, define your relationships
correctly (using the DataRelationships class, through the Relationships
property on the DataSet).

Finally, bind the combobox to the relationship, and not to the data
table, and when you change the record position in the first table (which has
the primary key), the data in the associated comboboxes will change.

Hope this helps.
 
i can't add an item after the datasource is set...the following code throws
an exception trying to execute the 2nd line:
cbExchange.DataSource = cb.DataSource;
cbExchange.Items.Add( "" );
cbExchange.ValueMember = "exchange_id";
cbExchange.DisplayMember = "symbol";

If i add it before the datasource, it doesn't really "stick"..as the
datasource clears all then adds the items in the datatable/dataset.
Doesn't this seem like a lot of hoops to jump thru though..should there be
an easier way? (i hope this is a bug.)
any other suggestions?

Nicholas Paldino said:
Dino,

You actually don't have to do anything in order to handle this scenario.
What you want to do is load the data from the three tables into one data set
(three separate tables). Once you do that, define your relationships
correctly (using the DataRelationships class, through the Relationships
property on the DataSet).

Finally, bind the combobox to the relationship, and not to the data
table, and when you change the record position in the first table (which has
the primary key), the data in the associated comboboxes will change.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dino L. said:
Hi,

I have a problem, and I just cant fint a good way to solve it.
I have 3 tables in my DB
Table1
-------
IDTable1
...

Table2
-------
IDTable2
IDTable1 (forign key -> Table1)
...

Table3
-------
IDTable3
IDTable2 (forign key -> Table2)
...

Now, on form load I need to fill one combo box with data from table1. That
works good, I just create dataset, fill it with tables, and fill combo
with IDTable1.

But now,
When someone choose one IDTable1 from first combo I have to fill second
combo with data from Table2 (according to forign key
Table2.IDTable1=Table1.IDTable1)??? Also when he choose another IDTable1
from first combo, datas in second combo should also change. (same way is
for 3th combo).

Which event on combo should I use, how to fill second combo after that
event ?!?
 
if i do the following, i can get a blank row. but man, what a kludge!

DataTable tmpDT = cb.DataSource;
DataRow nr = tmpDT.NewRow( );
tmpDT.Rows.InsertAt( nr, 0 );
tmpDT.DefaultView.Sort = "code";
cbExchange.DataSource = tmpDT; // cb.DataSource;
cbExchange.ValueMember = "exchange_id";
cbExchange.DisplayMember = "symbol";


Nicholas Paldino said:
Dino,

You actually don't have to do anything in order to handle this scenario.
What you want to do is load the data from the three tables into one data set
(three separate tables). Once you do that, define your relationships
correctly (using the DataRelationships class, through the Relationships
property on the DataSet).

Finally, bind the combobox to the relationship, and not to the data
table, and when you change the record position in the first table (which has
the primary key), the data in the associated comboboxes will change.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dino L. said:
Hi,

I have a problem, and I just cant fint a good way to solve it.
I have 3 tables in my DB
Table1
-------
IDTable1
...

Table2
-------
IDTable2
IDTable1 (forign key -> Table1)
...

Table3
-------
IDTable3
IDTable2 (forign key -> Table2)
...

Now, on form load I need to fill one combo box with data from table1. That
works good, I just create dataset, fill it with tables, and fill combo
with IDTable1.

But now,
When someone choose one IDTable1 from first combo I have to fill second
combo with data from Table2 (according to forign key
Table2.IDTable1=Table1.IDTable1)??? Also when he choose another IDTable1
from first combo, datas in second combo should also change. (same way is
for 3th combo).

Which event on combo should I use, how to fill second combo after that
event ?!?
 
Back
Top