Maybe this is a very simple question for you .NET guys but since I come from a MFC C++ dev teams, this behavior all got our dev team wondering what was going on. (In MFC, when we create a dropdown datasource, the source is shared but the value of each dropdown is not affected)
It’s a very simple situation. I have 3 dropdownlist in my form that ask for streets. The street you are on and the others 2 closest streets. Each of these dropdownlist have the same datasource. So what we did is that (by the way ComboList is just a class that inherit ListItem with some more functions)
//Combo Street
ComboList cboNomRue = new ComboList();
Tools.GetCombo(cboNomRue, Tools.ComboTypeRt.RT_NOM_RUE, true, true);
ddlNomRue.DisplayMember = "Numero";
ddlNomRue.ValueMember = "ID";
ddlNomRue.DataSource = cboNomRue;
//Combo Street From
ddlDe.DisplayMember = "Numero";
ddlDe.ValueMember = "ID";
ddlDe.DataSource = cboNomRue;
//Combo Street To
ddlA.DisplayMember = "Numero";
ddlA.ValueMember = "ID";
ddlA.DataSource = cboNomRue;
Using this code, when the user change the value in ddlNomRue, the 3 dropdownlist change to that value! So to fix that, I did that.
//Combo Street
ComboList cboNomRue = new ComboList();
Tools.GetCombo(cboNomRue, Tools.ComboTypeRt.RT_NOM_RUE, true, true);
ddlNomRue.DisplayMember = "Numero";
ddlNomRue.ValueMember = "ID";
ddlNomRue.DataSource = cboNomRue;
ComboList cboNomRue2 = new ComboList(cboNomRue);
ComboList cboNomRue3 = new ComboList(cboNomRue);
//Combo Street From
ddlDe.DisplayMember = "Numero";
ddlDe.ValueMember = "ID";
ddlDe.DataSource = cboNomRue2;
//Combo Street To
ddlA.DisplayMember = "Numero";
ddlA.ValueMember = "ID";
ddlA.DataSource = cboNomRue3;
I don’t like this since it’s duplicating variables when only one is really needed. Is this the good way of dealing with this situation or is there anything else I could do?
Thanks
I am not clear about the ComboList Class used by you, but if you use the List to store the item which is to be displayed in comboBox is the better option.
The way adopted by you is not good because of creating object again and again. To overcome this, use List and just call it function AsReadOnly();
Example:
But if you must have to use the Object of ComboList then implement the interface icloneable in ComboList or use any other way to create a shallow copy.