开发者

C# 2 ComboBox DropDownList showing same values

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"))开发者_C百科;

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

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


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.


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);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜