سؤال

I have two comboboxes which I populate using my GetAllCities() method in the CtrlMap.

My idea is, whenever I select another city on the ddFrom it should databind all the cities to ddTo (and later on remove the exact same selected, so user won't be able to select same city as point From and To).

However, Whenever I select something on ddFrom, ddTo populates (as it should), but SelectedIndex gets the same as the ddFrom. Same goes in the opposite way. If I select a city, lets say New York on ddTo it is also selected on ddFrom.

In the GUINewBooking.Designer.cs there's only this event handler registered: this.ddFrom.SelectedIndexChanged += new System.EventHandler(this.ddFrom_SelectedIndexChanged);

ddTo has no event handler registered. Any ideas?

public partial class GUINewBooking : Form
{
    private CtrlMap ctrlMap;

    public GUINewBooking()
    {
        InitializeComponent();

        ctrlMap = new CtrlMap();

        ddFrom.DataSource = ctrlMap.GetAllCities();
        ddFrom.DisplayMember = "name";
    }

    private void ddFrom_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddTo.DataSource = ctrlMap.GetAllCities();
        ddTo.DisplayMember = "name";
    }
}
هل كانت مفيدة؟

المحلول 2

The answer can be found Strange behavior of Windows Forms combobox control

Each combobox DataSource property should be assigned to a different BindingSource object.

Example:

cmbDataType1.DataSource = new BindingSource(datasource, "");
cmbDataType2.DataSource = new BindingSource(datasource, "");

Or in my particular case:

ddFrom.DataSource = new BindingSource(ctrlMap.GetAllCities(), "");
ddTo.DataSource = new BindingSource(ctrlMap.GetAllCities(), "");

نصائح أخرى

I believe it's because you are using the same data source. You might need to

private void ddFrom_SelectedIndexChanged(object sender, EventArgs e)
{
    CtrlMap ctrlMapTo = new CtrlMap();
    ddTo.DataSource = ctrlMap2.GetAllCities();
    ddTo.DisplayMember = "name";
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top