How to create a datasource to a combobox?

V

Vanessa

Hi all!

I need do create a combobox just with related rows, and they are in a third
table.


Explaining better: I have a table of salesmen, another of customers and
another one with relationship of salesmen X customers. When I select a
salesman, I want only to show the customers that are related to him.

Does it have a way to create one datatable with the customers related to the
salesman to be used in the datasource of the combobox?

Thank you in advance,
Vanessa
 
D

dlm@bypsoft

Hi Vanessa,

your problem is a common problem and I'm wonderng that nobody has responded to you. The solution is simple, if I got it correctly.
Let's say you want to have two combo boxes, one with Salesman's and the second one with Customers which belong to the selected salesman in the first combo box. To fullfill this you will need one DataSet called dsSales. For both ComboBexes you will need one BindingSource for each: bindingSourceSalesman and bindingSourceSalesmanCustomers. Your dataset has to contain two data tables tableSalesman and tableSalesmanCustomers where salesman has been filld from sql statement like
"select salesmanId, salesmanName from salesman"
and the second one
"select salesmanid, customerId, customerName from customers c inner join salesmanXcustomers s on c.salesmanId = s.salesmanId"
So, after filling your DataTables and adding them to the dsSales you need to add foreign key relations between those two DataTables on a salesmanId (note that you have it on both tables). Assume name of this relations is "FK_Salesman_X_Customers"

Next steps are related to the BindongSources. We are telling them what and how to bind.
bindingSourceSalesman.DataSource = dsSales; //your DataSet
bindingSourceSalesman.DataMember = "tableSalesman"; //name of the table - string

The second one, bindingSourceSalesmanCustomers will have as DataSource our bindingSourceSalesman, not our DataSet dsSales and DataMember will not be set to any table but to the foreign key relations name you set between these two tables.

bindingSourceSalesmanCustomers.DataSource = bindingSourceSalesman;
bindingSourceSalesmanCustomers.DataMember = "FK_Salesman_X_Customers";

On this way, once you are changing selection to comboBoxSalesman available values will be updates in comboBoxSalesmaCustomers.

Hope this helps.
Best regards,

Best regards,
dlm@bypsoft
Compare SQL Server, MySQL and Oracle for free - DBTyP.NET
www.bypsoft.com
 
V

Vanessa

Thank you very much! It help me a lot!

Vanessa


dlm@bypsoft said:
Hi Vanessa,

your problem is a common problem and I'm wonderng that nobody has responded to you. The solution is simple, if I got it correctly.
Let's say you want to have two combo boxes, one with Salesman's and the second one with Customers which belong to the selected salesman in the first combo box. To fullfill this you will need one DataSet called dsSales. For both ComboBexes you will need one BindingSource for each: bindingSourceSalesman and bindingSourceSalesmanCustomers. Your dataset has to contain two data tables tableSalesman and tableSalesmanCustomers where salesman has been filld from sql statement like
"select salesmanId, salesmanName from salesman"
and the second one
"select salesmanid, customerId, customerName from customers c inner join salesmanXcustomers s on c.salesmanId = s.salesmanId"
So, after filling your DataTables and adding them to the dsSales you need to add foreign key relations between those two DataTables on a salesmanId (note that you have it on both tables). Assume name of this relations is "FK_Salesman_X_Customers"

Next steps are related to the BindongSources. We are telling them what and how to bind.
bindingSourceSalesman.DataSource = dsSales; //your DataSet
bindingSourceSalesman.DataMember = "tableSalesman"; //name of the table - string

The second one, bindingSourceSalesmanCustomers will have as DataSource our bindingSourceSalesman, not our DataSet dsSales and DataMember will not be set to any table but to the foreign key relations name you set between these two tables.

bindingSourceSalesmanCustomers.DataSource = bindingSourceSalesman;
bindingSourceSalesmanCustomers.DataMember = "FK_Salesman_X_Customers";

On this way, once you are changing selection to comboBoxSalesman available values will be updates in comboBoxSalesmaCustomers.

Hope this helps.
Best regards,

Best regards,
dlm@bypsoft
Compare SQL Server, MySQL and Oracle for free - DBTyP.NET
www.bypsoft.com
 

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