c# - 级联三个组合框
问题描述
我的 Windows 窗体有三个组合框。CmdCountry 引用 CmdContinent 的子表,CmdCity 引用 CmdCountry 的子表。
但是当我尝试通过编码从 cmbContinent_SelectedIndexChanged 获取 ContinentId 时,出现“格式错误”之类的错误。
int ContId = Convert.Into32(cmbContinent.SelectedValue.ToString());
我的前两个组合框如下:
private void Continent()
{
var continent = (from u in db.Continent
select new { u.ContinentName, u.ContinentId }
).ToList();
cmbContinent.DataSource = continent;
cmbContinent.DisplayMember = "ContinentName";
cmbContinent.ValueMember = "ContinentId";
}
private void cmbContinent_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbContinent.SelectedValue.ToString() != null)
{
int ContId = Convert.Into32(cmbContinent.SelectedValue.ToString()); // Here I get the error
var country = (from u in db.Country
where u.ContinentId == ContId
select new { u.CountryId, u.CountryName }).ToList();
cmbCountry.DataSource = country;
cmbCountry.DisplayMember = "CountryName";
cmbCountry.ValueMember = "CountryId";
}
}
并在构造函数中加载第一个组合框大陆:
public NewAccount()
{
InitializeComponent();
Continent();
}
如果有人可以提供帮助,我将不胜感激。
解决方案
我在我当前的项目中使用了一个 ComboBox,我做了类似的事情。
int valueMax = Convert.ToInt32(cbxNumberValues.SelectedItem);
对于您的代码,这里应该可以工作。请注意,我几乎没有进行任何修改以进行更常规的编码。
int contId = Convert.ToInt32(cmbContinent.SelectedItem);
我还建议使用“cbx”或“cmb”而不是“cmd”,因为它通常会说命令。但这不是很重要
推荐阅读
- multithreading - 当请求增加时,WCF 请求时间增加
- adsense - AdSense 广告未插入网站的空白区域
- sql - 按日期范围划分 SQL 分区数据,忽略日期间隔和周末
- php - PHP多维数组 - 删除部分重复并加起来
- c# - C# 中的 Arduino 回调捕获
- r - 将 checkBoxGroup 写入文件并从中读取
- django - 谷歌的材料设计与 Django?(不带 node.js)
- sql - 如果将日期过滤器从字符串文字更改为日期变量,性能会变差
- javascript - 主干视图事件未绑定到多个模板
- python-3.x - 熊猫删除每个组中每个元素在列上具有相同值的组