I have a datagridview with 3 comboboxes.
When in combobox 1 (=Rownumber = 1) AA is selected then at runtime combobox2 has to be filed with some value.
This value is comming from a DataTable.
When some value is selected in combobox2 (Also RowNumber = 1), then at runtime combobox3 (also RowNumber = 1) has to be filled with some value also comming from a DataTable.
Above means that when combobox1 where the columnindex is 0 has the value:
AA
BB
CC
When AA is selected ComboBox2 where the columnindex is 0 must have the value (ComboBox2 is must be filled at runtime and the values are comming from a DataTable):
11
22
33
When 11 is selected ComboBox3 where the columnindex is 0 must have the value (ComboBox3 is must be filled at runtime and the values are comming from a DataTable):
Name
LastName
Address
City
THEN THE NEXT ROW
At RowNumber 2 ComboBox1 has the value:
AA
BB
CC
when BB is selected, ComboBox2 also at RowNumber 2 must have the value (ComboBox2 is must be filled at runtime and the values are comming from a DataTable):
Pipo
Peppie
Clown
when Pipo is selected, ComboBox3 also at RowNumber 2 must have the value (ComboBox3 is must be filled at runtime and the values are comming from a DataTable):
Yes
No
Maybe
I have tried a lot but nothing works.
Is above possible and how in C#?
Remco van HeckPosted Oct 29, 2010, 1:38 AM
Sam HobbsPosted Oct 28, 2010, 4:26 PM
It is relatively easy to do using bound controls but you need to learn about bound controls if you do not already know. You did not state explicitly if you are using bound controls. If you are not using bound controls then the solution seems easy to me; you just need to handle the event of the selection changing for each ComboBox; do you know how to do that?
Using bound controls, generally speaking, you need to handle the event of the selection changing from a ComboBox and in the handler function, set a filter (see BindingSource.Filter Property) for the data source for the next ComboBox. So when ComboBox1's selection changes, set a filtter for ComboBox2 and clear ComboBox3, or something like that.
Remco van HeckPosted Oct 28, 2010, 4:32 AM
Remco van HeckPosted Oct 27, 2010, 2:13 AM
Suthish NairPosted Oct 27, 2010, 12:06 AM
Remco van HeckPosted Oct 26, 2010, 2:57 PM
dataGridView1.Columns.Insert(0, ComboBoxDB);
Suthish NairPosted Oct 26, 2010, 2:35 PM
Remco van HeckPosted Oct 26, 2010, 1:09 PM
You understood it wrong. It is clear to me how to fill a combobox, What I don't know is how to fill them differently each row.
So as in my example: row 1 --> combobox1 has the value abc, def, hij --> combobox2 has the value table1, table2 etc... (when abc is selected in combobox1) combobox3 has the values: name, address etc... (when table1 is selected in combobox2)
row2 --> combobox1 has the value abc, def, hij --> combobox2 has the value TV, Radio, Laptop etc... (when def is selected in combobox1) combobox3 has the values: button, speaker, etc.. (when Radio is selected in combobox2).
So putting values/items into a combobox is not the problem, but putting different comboboxes in each row is.
JurePosted Oct 26, 2010, 7:13 AM
I'm wet behind ears myself, but I'm sure someone much more experienced will tell you too that it's good to separate GUI, data and the connection between GUI and data.
Just to clarify, my understanding is that you don't know how to make a certain selection of the combobox items get the proper data from your database (algorithm-wise, not syntax-wise). I'm really sorry for all the confusion if that's not true.
It might help if you upload your project and a sample of your database.
About putting items into combobox, use Items property, which is a collection-type:
my_combobox.Items.Add(...);
my_comboboc.Items[0] = ...
..etc..
Remco van HeckPosted Oct 26, 2010, 5:36 AM
I still don't see why to use delegates and why a good design?
It's simple to put a DataTable in a Combobox and the design is clear because there are a few databases with tables and this tables has columns, nothing more nothing less. Also with Delegates, please explain me how to put values in each Combobox because that's where the difficult is. If I know how then I can also populate them with the values from my DataTables.
JurePosted Oct 26, 2010, 4:43 AM
In my opinion you should really focus on the algorithm first, leave the practical stuff, such as filling your datagridview, for later. I myself would implement model-view-controller pattern.
Manikavelu VelayuthamPosted Oct 26, 2010, 4:16 AM
You can achieve this using JSON call.
Based on the first combo box selection, JSON call will hit the database and file the second combo box.
Remco van HeckPosted Oct 26, 2010, 3:43 AM
I can't say, It depense on how many tables a database has. Because it show's the name of the tables and after selected on of the tables it shows the columns. I think that companies don't have databases with more than 200 tables so say the values will between 1 and 200.
Manikavelu VelayuthamPosted Oct 26, 2010, 3:24 AM
Becuase if the items come around 100 to 200, we can have it in array. Based on the selection, we can select or fill the items in other combo box using the normal javascript.
Remco van HeckPosted Oct 26, 2010, 2:29 AM
I tried code like this -->
dataGridView1.Columns.Insert(0, ComboBoxDB);
dataGridView1.Rows[rowNr1].Cells[colNr + 1].Value = Convert.ToString(cmbBox.SelectedIndex);
What ever I tried It doesn't work, so please send me code how to do it.
I only think that it's not possible what I want? But if you think it's possible please let me know.
JurePosted Oct 25, 2010, 6:52 PM
But be careful when writing the whole thing... For example, 3 options in each ComboBox will give you 27 different combinations.
This can quickly turn into a spaghetti code, so I recommend you to use custom delegates or events, and separate code heavily into functions to avoid any copy-pasting and to make it possible to dynamically construct one large function out of little ones.
This article might help you with evaluating multiple variables, but I don't recommend making a switch with 27+ cases.
Upload your project if you get stuck.