【问题标题】:ComboBox SelectedValue always null, cannot be setComboBox SelectedValue 始终为空,无法设置
【发布时间】:2014-01-09 22:18:41
【问题描述】:

我正在开发一个 C#.Net 4.0 Winforms 应用程序。我遇到了一些困难,两个组合框总是为它们的 SelectedValue 属性返回 NULL

我在这个表格上有三个组合。第一个组合是数据绑定到一个 BindingSoucce,而后者又具有一个类型化的 DataTable 作为它的 DataSource。该组合按预期工作。

其他两个组合绑定到本地通用数据表。这些是失败的。 有问题的组合是 cmbDays 和 cmbYears。他们的数据表通过简单的循环填充到代码中。两个表都包含人口调用结束时的数据。 dtDaysLU 包含字符串列“Day”。 dtYearsLU 包含字符串列“年份”。非常简单。

绑定 BindingSource 和组合的代码如下所示:

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 如下所示:

public void DataBindComboBox(ComboBox combo, BindingSource bindingSource, string displayMemberName, string valueMemberName)
{
    combo.DataBindings.Clear();
    combo.DisplayMember = displayMemberName;
    combo.ValueMember = valueMemberName;
    combo.DataSource = bindingSource;
}

您可以看到,这一切都不是火箭科学。我确实想指出组合框属性的设置顺序——根据 MSDN 的建议:

  • 显示成员
  • 价值成员
  • 数据源

在代码中,我尝试像这样设置这些组合的 SelectedValue:

this.cmbMonth.SelectedValue = this._permitMonthNum;
this.cmbDay.SelectedValue = this._permitDay.ToString();
this.cmbYear.SelectedValue = this._permitYear.ToString();

请注意,我在设置这两个组合的 SelectedValue 时对成员数据执行了 .ToString(),因为填充其基础表的代码使用字符串类型:

this._dtDays.Columns.Add("Day", typeof(string));

所以我知道这不是数据类型不匹配。

调试显示cmbDaycmbYear 组合的.SelectedValue 属性为空。

【问题讨论】:

  • combobox dropdownstyle 属性是什么?
  • _permitDay 的值是多少,是否在 _bsDaysLU 列表中?您一直在清除 DataBindings,但从未显示添加了任何 DataBindings。
  • Steve:样式是 DropDown。
  • LarsTech:绑定被添加到调用中:this._manipulator.DataBindBindingSource(this._bsMonthsLU, this._ds.tblMonthsLU); this._manipulator.DataBindingSource(this._bsDaysLU, this._dtDays); this._manipulator.DataBindingSource(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");
  • @LarsTech:嗯 - 我会看看 _permitDay 的值,并确保它在 BindingSource 列表中。可悲的是,我实际上并没有检查过。我们都知道假设会把我们带到哪里。

标签: c# combobox selectedvalue


【解决方案1】:

好的@LarsTech - 你走在正确的道路上。似乎这个逻辑分支绕过了填充我的数据表的函数。所以,是的 - 我的列表中没有任何内容可以匹配。

非常感谢你的新鲜眼睛。我一直认为这些表有来自之前调用的数据,以至于我没有意识到要进行验证。

谢谢!

库尔特

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 2013-12-01
    • 2018-05-24
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    相关资源
    最近更新 更多