将 ComboBox 绑定到 IList 并使用 SelectedValue

发布于 2024-07-11 03:54:52 字数 874 浏览 13 评论 0原文

我有一个 ComboBox 设置如下,其中 KVPList 是一个 IList (如果重要的话,是 KeyValuePair ):

comboBox.DisplayMember = "Value";
comboBox.ValueMember = "Key";
comboBox.DataSource = KVPList;

然后,我设置了与 SelectedValue 的绑定,绑定到 BindingSource (绑定到 DataSet)。 无论出于何种原因,当显示表单时,组合框总是显示为空白。 但是,它已正确填充(IList 的值显示正常并且可以选择)。

现在,我已尽力进行跟踪,似乎在绑定时最初正确设置了 SelectedValue,但随后在某个地方它被重置为 null。 我也玩过调用顺序,但无济于事。

任何人都可以阐明这一点或提出解决方法吗?

作为记录,在同一个表单上,我在同一表单上有另一个 ComboBox,其 SelectedValue 绑定到相同的 BindingSourceDataSource 是一个DataSet,而不是一个IList,它的工作方式就像一个魅力。 从 IList 中创建 DataTable 可能是一种选择,但这似乎会产生大量额外开销; 我正在从枚举生成 IList

I have a ComboBox set up as follows, where KVPList is an IList (of KeyValuePair if it matters):

comboBox.DisplayMember = "Value";
comboBox.ValueMember = "Key";
comboBox.DataSource = KVPList;

I then have set up a binding with SelectedValue, binding to a BindingSource (to a DataSet). For whatever reason, the combo box always turns up blank when the form is displayed. It is properly populated, however (the values of the IList show up fine and can be selected).

Now, I've tried my best to trace through, and it appears to initially set the SelectedValue correctly when bound, but then somewhere along the way it gets reset to null. I've played with the order things get called as well, to no avail.

Can anyone shed some light on this or suggest a workaround?

For the record, on the same form, I have another ComboBox on the same form, with its SelectedValue bound to the same BindingSource. The DataSource is a DataSet, not an IList and it works like a charm. It might be an option to make a DataTable from the IList, but it seems like a whole lot of extra overhead; I'm generating the IList from an enumeration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-07-18 03:54:52

哎哟。 在这个问题上浪费了半天时间后,我已经弄清楚了。 这完全是我的错误。

KVPList 设置为 KeyValuePair<shortIList, string>,但数据字段的类型为int。 本质上,数据绑定将触发,并设置 SelectedValue 属性。 然后,DisplayMemberValueMember 绑定将触发,并再次检查 SelectedValue。 由于 ValueMember 的类型为 short,而不是 int,因此它找不到匹配项,因此将其设置为 null。

装箱和拆箱肯定发生了一些有趣的事情,但我现在太累了,无法理解为什么。

我会保留这个问题,以防其他人遇到同样的问题。 很难追踪,因为我希望它要么尝试强制转换,要么抛出异常,而不是默默地返回 null。 毕竟,shortint 都是值类型,并且我上次检查的 (int)10 == (short)10 为 true。

Ouch. After basically half a day wasted on this one, I've figured it out. It was completely an error on my part.

The KVPList was set to an IList of KeyValuePair<short,string>, but the data field is of type int. Essentially, the databinding would fire, and set the SelectedValue property. Then the DisplayMember and ValueMember bindings would fire, checking the SelectedValue again. Since the ValueMember is of type short, not int, it wouldn't find a match and thus set it to null.

Something funny must be happening with boxing and unboxing, but I'm too tired to understand why right now.

I'll leave this question up in case someone else runs into the same issue. It's hard to track down because I would expect it to either try to cast or throw an exception, not silently go null. After all, short and int are both value types and last I checked (int)10 == (short)10 holds true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文