C#combobox选择的索引改变了另一个组合框中的旧值

时间:2012-06-26 14:27:12

标签: c# .net combobox

我在这些链接上找到了我最接近的问题:

  

ComboBox has its old value after Clear()
  Why selected index fires only once the Index of the ListItem is changed?

我有4个组合框:cmbcountrycmbstatecmbdistrictcmbcitycmbcountry通过GetCountry()方法填充加载事件。 cmbstate填充GetState(countryid)方法,该方法将cmbcountry的selectedvalue作为参数,并返回cmbdistrictcmbcity的相关状态列表等。 。

问题是在cmbState中选择不同的项目时,cmbDistrct没有填充正确的项目。

.E.g。州:“madhya pradesh”,“Utter pradesh”,“Rajsthan”

对于“Rajsthan”来说,cmbdistrict充满了相关价值,但对于“Utter pradesh”来说,cmbdistrict仍然具有旧价值。

以下是相关代码:

private void TestForm1_Load(object sender, EventArgs e)
{
    cmbCountry.ItemSource = Lookups.Lookup.GetCountries();
}

private void cmbCountry_SelectionChangeCommitted(object sender, EventArgs e)
{
    cmbState.Text = "";
    cmbState.Clear();
    cmbState.SelectedIndex = -1;
    cmbState.SelectedItem = null;
    //cmbState.Items.Clear();
    int countryId = Convert.ToInt32(cmbCountry.SelectedValue);
    cmbState.ItemSource = Lookups.Lookup.GetStates(countryId);
}

private void cmbState_SelectionChangeCommitted(object sender, EventArgs e)
{
    cmbDistrict.Text = "";
    cmbDistrict.Clear();
    cmbDistrict.SelectedIndex = -1;
    cmbDistrict.SelectedItem = null;
    //cmbDistrict.Items.Clear();            
    int stateId = Convert.ToInt32(cmbState.SelectedValue);
    cmbDistrict.ItemSource = Lookups.Lookup.GetDistricts(stateId);
}

private void cmbDistrict_SelectionChangeCommitted(object sender, EventArgs e)
{
    cmbCity.Text = "";
    cmbCity.Clear(); 
    cmbCity.SelectedIndex = -1;
    cmbCity.SelectedItem = null;
    //cmbCity.Items.Clear();
    int DistrictId = Convert.ToInt32(cmbDistrict.SelectedValue);
    cmbCity.ItemSource = Lookups.Lookup.GetCities(DistrictId);
}

private void cmbBank_SelectionChangeCommitted(object sender, EventArgs e)
{
    int bankId = Convert.ToInt32(cmbBank.SelectedValue);
    cmbControlBranch.ItemSource = Lookups.Lookup.GetBranches(bankId);
}

我有以上所有方法来清除组合框中的先前数据...如果我正在使用它,则items.clear()会出错。

请告诉我是否还有其他相关参考资料。

0 个答案:

没有答案