我正在开发一个C#.Net 4.0 Winforms应用程序,并且遇到了一些困难,两个组合框总是返回NULL来获取它们的SelectedValue属性。
我在这张表格上有三个组合。第一个组合体是数据库到一个BindingSoucce,而后者又有一个类型化的DataTable作为它的DataSource。这个组合就像预期的一样。
另外两个组合被绑定到本地的泛型DataTables。这些都是失败的。令人反感的组合是cmbDays和cmbYears。它们的数据表是通过简单循环以代码填充的。这两个表在其人口调用结束时都包含数据。dtDaysLU包含字符串列‘Day’。dtYearsLU包含字符串列‘年份’。很简单。
绑定BindingSources和combos的代码如下所示:
代码语言:javascript运行复制private void databindPermitDateCombos()
{
this.cmbMonth.DataBindings.Clear();
this.cmbDay.DataBindings.Clear();
this.cmbYear.DataBindings.Clear();
this._bsMonthsLU.DataSource = null;
this._bsDaysLU.DataSource = null;
this._bsYearsLU.DataSource = null;
this._manipulator.DataBindBindingSource(this._bsMonthsLU, this._ds.tblMonthsLU);
this._manipulator.DataBindBindingSource(this._bsDaysLU, this._dtDays);
this._manipulator.DataBindBindingSource(this._bsYearsLU, this._dtYears);
this._manipulator.DataBindComboBox(this.cmbMonth, this._bsMonthsLU, "Month", "MonthNumber");
this._manipulator.DataBindComboBox(this.cmbDay, this._bsDaysLU, "Day", "Day");
this._manipulator.DataBindComboBox(this.cmbYear, this._bsYearsLU, "Year", "Year");
}…其中_manipulator.DataBindComboBox看起来是这样的:
代码语言:javascript运行复制public void DataBindComboBox(ComboBox combo, BindingSource bindingSource, string displayMemberName, string valueMemberName)
{
combo.DataBindings.Clear();
combo.DisplayMember = displayMemberName;
combo.ValueMember = valueMemberName;
combo.DataSource = bindingSource;
}你可以看到,所有这些都不是火箭科学。我确实想指出按照MSDN建议设置combobox属性的顺序:
显示成员价值成员DataSource
在代码中,我试图设置这些组合的SelectedValue,如下所示:
代码语言:javascript运行复制this.cmbMonth.SelectedValue = this._permitMonthNum;
this.cmbDay.SelectedValue = this._permitDay.ToString();
this.cmbYear.SelectedValue = this._permitYear.ToString();注意,在设置这两个组合的SelectedValue时,我对成员数据执行一个SelectedValue,因为填充它们的底层表的代码使用字符串类型:
this._dtDays.Columns.Add("Day", typeof(string));
所以我知道这不是数据类型错配。
调试显示cmbDay和cmbYear组合的cmbDay属性为null。