Combobox和不幸的财产名称 [英] Combobox and Unfortunate Property Names

查看:68
本文介绍了Combobox和不幸的财产名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Winform上有一个组合框。

I have a combobox on a Winform.

我希望组合框绑定到我的数据库中的一个字段。 (以及我表单上的很多其他控件)。

I want the combobox to be bound to a field in my database. (along with a lot of other controls on my form).

我想从我创建的自定义列表中加载列表。  

I want to load the list from a custom list I have created.  

            //load the list
            cbPlant1.DataSource = orderPlantsList.items;
            cbPlant1.DisplayMember = "Display";
            cbPlant1.ValueMember = "Value";

以这种方式加载列表是有效的(只要我在填充数据表之前加载它)。

Loading the list this way works (as long as I load it before the datatable is filled).

现在我试图将组合框挂起以便

Now I am trying to hook the combobox up so that


  1. 当我在数据表中加载记录时,数据显示在字段中,并且 
  2. 选择另一个项目会更改数据表中的数据。

我已经尝试通过在Databindings.SelectedItem-purchase_OrderBindingSource.plant中选择它来将数据库列添加到组合框中,在Designer.cs中看起来像这样:

I have tried adding the database column to the combobox by selecting it in Databindings.SelectedItem-purchase_OrderBindingSource.plant, which looks like this in the Designer.cs:

this.cbPlant1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.purchase_OrderBindingSource, "Plant", true));

这导致以下结果:


  1. 加载a记录结果为空的组合框。
  2. 下拉列表中包含正确的值。
  3. 保存数据会导致此错误: 其他信息:无法设置列'厂'。该值违反了此列的MaxLength限制。

我觉得这里不幸的是,对象属性都是关于"数据"的,没有清楚地显示什么是
列表数据,什么是目标表数据

The thing I find unfortunate here, is that the object properties all talk about "Data", without clearly showing what is List Data, and what is Target Table Data.

这里没有MS教程,我只能找到关于绑定列表数据的教程,而不是绑定的字段数据。

There is no MS tutorial on this, I can only find tutorials on binding the list data, not the bound field data.

(我知道我很接近,但我只是不明白)

(I know I'm close, but I just don't get it)

我宁愿生活在虚假的希望而不是虚伪的绝望中。

I'd rather live with false hope than with false despair.

推荐答案

尝试" SelectedValue"而不是" SelectedItem"

Try "SelectedValue" instead of "SelectedItem".


这篇关于Combobox和不幸的财产名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆