【发布时间】:2015-10-21 08:29:57
【问题描述】:
注意:这是一个普通的 WinForms 应用程序。没有 WPF 或 WCF 或其他任何东西。
大家好,我正在开发一个 WinForms (.NET 3.5) 应用程序,但遇到了一个问题。我正在尝试根据所选州获取城市(两者都在 2 个不同的 ComboBox-es 中,DropDownStyle 设置为 DropDownList)。
我已经使用SelectedValue 属性在SelectedIndexChanged 事件处理程序中实现了这个逻辑。 DataSource 是从 DB 方法返回的 DataTable,SelectedValue 返回 DataRowView 的实例。
在表单构造函数中,我填充状态组合框并使用SelectedIndex 属性以编程方式设置默认状态选择;然后它转到事件处理程序,正确执行,并为该州填充城市 ComboBox。
现在问题来了,当我更改选定状态构建后,当表单启动并运行时,使用我的鼠标。这再次转到事件处理程序,但SelectedValue 属性返回一个空引用。请帮忙。我附上下面的代码。
private void comboFindState_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView selectedState;
int selectedStateId;
DataTable citiesTable;
selectedState = comboFindState.SelectedValue as DataRowView;
if (selectedState != null) //Is true the first time around when the event is
//triggered due to programmatic change of the index.
//Then null afterwards, on change via mouse click.
{
selectedStateId = Convert.ToInt32(selectedState.Row["State Code"]);
citiesTable = DatabaseHelper.getStateCities(selectedStateId);
comboFindCity.DataSource = citiesTable; //Same binding for state ComboBox
//in the form's constructor;
comboFindCity.DisplayMember = "City"; //only here it says "State",
comboFindCity.ValueMember = "City Code";// and here it says "State Code".
comboFindCity.SelectedIndex = 0; //Same thing in the form's constructor for
//setting default selected index of state ComboBox.
}
else
{
//just populates an error TextBox saying 'No Cities Found'
}
}
请注意,所有这些都发生在状态 ComboBox 中,该状态已被填充。城市ComboBox第二次都没有进入范围,所以不存在数据库问题。
编辑:仅供参考,我从绝对开始为 tcomboFindState 设置了ValueMember 属性。所以这不是它不起作用的原因。另请注意,它第一次运行正常,因此证明 ValueMember 设置正确。
【问题讨论】:
-
您可以尝试将 comboFindState.SelectedValue 替换为 comboFindState.SelectedItem 吗?
-
瞧!有用!你先生/女士,应该被称为我的救世主。但是,我仍然不明白为什么
SelectedValue第二次没有工作。对这个问题有什么想法吗? -
正如 Fabio 解释的那样,问题出在 DataSource 属性的使用上。您需要设置 ValueMember。
-
我已经设置了
ValueMember属性。与 Fabio 的代码的唯一区别是我首先设置了DataSource,然后设置了ValueMember。这似乎是合乎逻辑的事情,否则如果DataSource为空,编译器将如何知道“状态代码”的含义?如果不同,请解释。 -
您可以在
DataSource之前或之后两种方式设置ValueMember。如果您在分配DataSource之后设置ValueMember,则可能会使用数据源属性/列对新的ValueMember进行一些额外的检查。
标签: c# winforms combobox .net-3.5