views:

269

answers:

2

Hi,

In my code I have 2 ComboBox DropDownLists, created by the code below. The problem is that when the value of the DropDownlist get changed, the other one value also changes. Can you please help me how I can solve this problem?

public class Translate
{
    public string CountryName { get; set; }
    public string CountryCode { get; set; }
}

IList<Translate> languages = new List<Translate>();
languages.Add(new Translate("Select", ""));
languages.Add(new Translate("English", "en"));
languages.Add(new Translate("French", "fr"));
languages.Add(new Translate("Spain", "es"));

ddlFrom.DataSource = languages;
ddlFrom.DisplayMember = "CountryName";
ddlFrom.ValueMember = "CountryCode";

ddlTo.DataSource = languages;
ddlTo.DisplayMember = "CountryName";
ddlTo.ValueMember = "CountryCode"; 
+3  A: 

That's because you're pointing both dropdown lists to the same datasource. You need to make a second copy of languages to pass to ddlTo.DataSource.

Nader Shirazie
thanks.Solved by doing the following: IList<Translate> languagesFrom = new List<Translate>(); languagesFrom.Add(new Translate("Select", "")); languagesFrom.Add(new Translate("English", "en")); languagesFrom.Add(new Translate("French", "fr")); languagesFrom.Add(new Translate("Spain", "es")); IList<Translate> languagesTo = new List<Translate>(languagesFrom);
mouthpiec
glad to be of service
Nader Shirazie
+2  A: 

When you use the IList as a data source, you're implicitly synchronizing access to the list, including the notion of a 'selected' item. You should be able to simply use:

        ddlFrom.Items.Clear();
        ddlTo.Items.Clear();
        foreach (var language in languages)
        {
            ddlFrom.Items.Add(language);
            ddlTo.Items.Add(language);
        }
Dan Bryant