Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
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
C#
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

1 solution

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[^]

ADDITION:

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).
 
Share this answer
 
v3
Comments
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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900