C# 如何使用多个组合框?
我正在C.Net中实现一个windows应用程序。我有三张桌子:国家、州、区。它们都与外键有关。 问题是在地区表单中,如果我选择一个countrycombobox,则相关状态不会显示在另一个combobox中。我为状态表使用selectedindexchange事件。但它表明 输入字符串的格式不正确 我将在下面给出一个代码。所以请帮帮我C# 如何使用多个组合框?,c#,winforms,combobox,C#,Winforms,Combobox,我正在C.Net中实现一个windows应用程序。我有三张桌子:国家、州、区。它们都与外键有关。 问题是在地区表单中,如果我选择一个countrycombobox,则相关状态不会显示在另一个combobox中。我为状态表使用selectedindexchange事件。但它表明 输入字符串的格式不正确 我将在下面给出一个代码。所以请帮帮我 private void DistrictMaster_Load(object sender, EventArgs e) { BEMaster objB
private void DistrictMaster_Load(object sender, EventArgs e)
{
BEMaster objBEMaster = new BEMaster();
BLMaster objBLMaster = new BLMaster();
cboCountryName.DataSource = objBLMaster.GetCountry();
cboCountryName.ValueMember = "CountryId";
cboCountryName.DisplayMember = "CountryName";
}
private void cboCountryName_SelectedIndexChanged_1(object sender, EventArgs e)
{
try
{
if (cboCountryName.SelectedValue.ToString() != "")
{
BEMaster objBEMaster = new BEMaster();
objBEMaster.CountryId = Convert.ToInt32(cboCountryName.SelectedValue);
BLMaster objBLMaster = new BLMaster();
cboStateName.DataSource = objBLMaster.GetStateByCountryId(objBEMaster);
cboStateName.DisplayMember = "StateName";
cboStateName.ValueMember = "StateId";
}
}
catch (Exception)
{
throw;
}
}
以上是否有任何错误,请给我回复。
谢谢。作为建议,电话线
如果cboCountryName.SelectedValue.ToString!=
可替换为:
if(!String.IsNullOrEmpty(cboCountryName.SelectedValue.ToString())
关于你的问题,从你的描述来看,不清楚问题在哪里,但我认为这一行是你的问题:
objBEMaster.CountryId = Convert.ToInt32(cboCountryName.SelectedValue);
相反,请使用该方法,而不是处理异常并检查问题是否为转换问题:
int myCountry;
bool result = Int32.TryParse(cboCountryName.SelectedValue, out myCountry);
if(result)
{
BEMaster objBEMaster = new BEMaster();
objBEMaster.CountryId = myCountry;
BLMaster objBLMaster = new BLMaster();
cboStateName.DataSource = objBLMaster.GetStateByCountryId(objBEMaster);
cboStateName.DisplayMember = "StateName";
cboStateName.ValueMember = "StateId";
}
在哪一行出现错误欢迎使用stackoverflow,请参见objBEMaster.CountryId=Convert.toint32cbocontryname.SelectedValue;