Conditional Dropdowns
if you select T1, T2 should only reflect values based on T1 and if you selected up to T2
then T3 should reflect only values based on T2
Hope someone can help I want to create a dropdown winforms c# 3 dropdowns to be exact
__________
|__________|<-- selection 1 - T1
__________
|__________|<-- selection 2 - T2
__________
|__________|<-- selection 3 - T3
my data will look like this
This data will come from a data source
and will look like below
T1 - Application
T2 - Adobe
T3 - Flash
----------------
T1 - Application
T2 - Adobe
T3 - Dreamweaver
---------------
T1 - Application
T2 - Adobe
T3 - Photoshop
---------------
T1 - Application
T2 - Microsoft
T3 - Word
---------------
T1 - Application
T2 - Microsoft
T3 - Excel
So with the above data fi you select Application on T1 the T2 should be either Adobe or Microsoft and if you select one of them it should only show the T3 relevant to T2
Any help would be appreciated.
4
Answers
You only bind the first combobox during design time, to the T1 values.
Then on a selectedindexchanged event of that combobox, you retrieve the selected item and and bind the second combobox with a filtered list based on that value.
You do the same with the selectedindexchanged of the second combobox and the binding of the 3rd combobox.
set
for first two drop downs. And handle their SelectedIndexChanged event. On page load event, bind the first drop down in following way.
Then on dd1_SelectedIndexChanged event use following code
and so on so forth..
Load data only in the first combo box.
SelectedIndexChanged
event handler for that comboBox, load data in second combo box; clear and disable 3rd, 4th…. combo boxes.SelectedIndexChanged
event handler for 2nd comboBox, load data in 3rd combo box; clear and disable 4th, 5th… combo boxesEdit: Sample code added
The sample code provided below is only for reference and by no means as per any guidelines
It is all depends on what this win forms will be used for. But in any case data binding will be on load to only T1 drop down.
In web you would use OnChange event and call JavaScript function to either use AJAX/jQuery to populate T2 box or just submit form to repopulate T2 with new values (bit harder and slower but will give you same result)
Same goes for T3 box which will be populated when T2 is changed.I would not use Postback because form will be submitted on every change and then you may lose data on other fields or you will have to verify that data repopulate all other fields properly.
On windows app you will have to use drop down events to do basically the same but in C# coding.