繁体   English   中英

ComboBox SelectedValue始终为null,无法设置

[英]ComboBox SelectedValue always null, cannot be set

我正在使用C#.Net 4.0 Winforms应用程序,并且在两个组合框始终为其SelectedValue属性返回NULL时遇到了一些困难。

我有这种形式的三个连击。 第一个组合是绑定到BindingSoucce的数据,而BindingSoucce又将类型化的DataTable作为其DataSource。 该组合按预期工作。

其他两个组合绑定到本地通用DataTable。 这些都是失败的。 令人讨厌的组合是cmbDays和cmbYears。 它们的数据表通过简单的循环以代码填充。 这两个表在其填充调用的末尾都包含数据。 dtDaysLU包含字符串列“ Day”。 dtYearsLU包含字符串列“ Year”。 很简单

绑定BindingSources和combos的代码如下所示:

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属性为null。

好吧@LarsTech-您走对了路。 似乎此逻辑分支绕过了填充我的数据表的功能。 所以,是的-我的列表中没有与之匹配的东西。

非常感谢您的新鲜关注。 我被表的上次调用中的数据所困扰,以至于我无法进行验证。

谢谢!

库尔特

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM