我在这些链接上找到了我最接近的问题:
ComboBox has its old value after Clear()
Why selected index fires only once the Index of the ListItem is changed?
我有4个组合框:cmbcountry
,cmbstate
,cmbdistrict
和cmbcity
。 cmbcountry
通过GetCountry()
方法填充加载事件。 cmbstate
填充GetState(countryid)
方法,该方法将cmbcountry
的selectedvalue作为参数,并返回cmbdistrict
和cmbcity
的相关状态列表等。 。
问题是在cmbState
中选择不同的项目时,cmbDistrct
没有填充正确的项目。
对于“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()会出错。
请告诉我是否还有其他相关参考资料。