Click here to Skip to main content
13,596,708 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi i am a newbie i am doing a small project in which there are two comboboxes in which first is of mobile company and second is of mobile model i want to update the list of model when the first combobox selection changes from one company to another, i mean when nokia is selected the it will show all the list of models so i have used datasource databinding of column for loading all the list of models in the second combobox i have made this
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)

       comboBox3.Text = "";

       if ("samsung" == comboBox4.SelectedItem.ToString())

           comboBox3.DataSource = table1BindingSource;
           comboBox3.ValueMember = "samsung";
           comboBox3.DisplayMember = "samsung";
       if ("htc" == comboBox4.SelectedItem.ToString())
           comboBox3.DataSource = table1BindingSource;
           comboBox3.ValueMember = "htc";
           comboBox3.DisplayMember = "htc";

there are three columns in the table, nokia, htc and samsung, nokia list of models contains 12 cells filled and htc contains 6 cells filled when i select the htc in the first combobox then 6 rows are seen in the combobox but 6 which are are also calculated and seen as null in the list of second combobox. how to avoid null values here is my problem
Posted 30-Dec-12 11:21am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I'm not sure if I understood the question correctly, but do you have all the companies in the same table with models and in separate columns.

If that is the case, I suggest following kind of design:
Manufacturer -table
- manufacturerid (generated, primary key)
- name

Model -table
- modelid (generated, primary key)
- manufacturerid (foreign key to manufacturer)
- name

Now you could list the manufacturers in the first combo box. Based on the selection you could filter the second combobox items using the manufacturerid as the criteria.

You could fetch all the models in the beginning and just use a DataView[^] to list only appropriate models per each manufacturer selection. The criteria would be defined using RowFilter[^]


Example for the data:

Manufacturer table
manufacturerid name
-------------- ----------
1              Nokia
2              Samsung
3              HTC

Model table
modelid manufacturerid name
------- -------------- ----------
1       1              C7
2       1              Lumia 900
3       1              Lumia 920
4       2              Galaxy S II
5       2              Galaxy S III
6       3              Desire X
7       3              Windows Phone 8X
8       3              One S

Now if Samsung is selected the rowfilter is set to define
manufacturerid = 2

so only two rows would be returned for the second combobox. The same mechanism applies to all manufacturers but the amount of rows is always different (the amount of models for that manufacturer).
shaikh-adil 30-Dec-12 18:09pm
i have used datasource for binding.
I have only mobile models list in the table. And the if loop works if samsung will be the selection the change the datasource to the samsung column and if htc is selected the change the model column to htc. I have a tabel and there are comlumns which are htc, nokia, samsung, in htc all the fields are filled with htc model and in samsung all the fields are filled with htc model. But the problem is when i filled htc lets say 15 models and then the corresponding rows for samsung and nokia will be null then if i select nokia in the first combobox then the second combobox is showing null values of all the 15 models i want it should be blank. Now you undestood sir?
sorry for my bad enlgish
Wendelius 30-Dec-12 18:20pm
So basically you have 15 rows in the table if any of the manufacturers has 15 models (nokia, htc or samsung)? If that's true and you hold the data in separate columns instead of separate rows, you'll get problems.

What I'm suggesting, eliminates the problem for null values in different columns since you always select rows based in foreign key values.

See the updated solution for an example for the data.
shaikh-adil 31-Dec-12 1:21am
nice sir,
manufacturerid (generated, primary key)??
whtat is this generated?
and how can i define row filter? what you have told is manufacturerid = 2? where to write this?
Wendelius 31-Dec-12 1:28am
By generated I mean a surrogate key[^]. In my opinion the best choice is to use an uniqueidentifier[^] for the column datatype with the default of NEWID.

To get the value when inserting a record, you can use the OUTPUT[^] clause.
shaikh-adil 31-Dec-12 1:35am
sir i have first timke heard this ,uniqueidentifier[^] NEWID. surrogate key[^]? can i use the normal things which i do for creating tables? an to update i used insert, update and delete statement?? i am using sql server here
Wendelius 31-Dec-12 1:43am
Of course you can, but I suggest getting familiar with surrogates. They make life much more easier in the future :-)

The surrogate key doesn't affect how you query or modify the table (except for the key column) so basically everything is done the same way. The main difference is just that the database generates the key, not the program...
shaikh-adil 31-Dec-12 1:47am
yup thank you sir, i have made relationship but where to write the code?
for example when the samsung is selected
what to write???
Wendelius 4-Jan-13 15:11pm
When a selection is made, get the key value from the combobox and then execute a select like
SELECT ... FROM Model WHERE ManufacturerId = @selectedidvalue

For an example about the parameters in the command, see[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02-2016 | 2.8.180621.3 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100