将 ComboBox 绑定到 IList 并使用 SelectedValue
我有一个 ComboBox
设置如下,其中 KVPList
是一个 IList
(如果重要的话,是 KeyValuePair
):
comboBox.DisplayMember = "Value";
comboBox.ValueMember = "Key";
comboBox.DataSource = KVPList;
然后,我设置了与 SelectedValue
的绑定,绑定到 BindingSource
(绑定到 DataSet
)。 无论出于何种原因,当显示表单时,组合框总是显示为空白。 但是,它已正确填充(IList
的值显示正常并且可以选择)。
现在,我已尽力进行跟踪,似乎在绑定时最初正确设置了 SelectedValue,但随后在某个地方它被重置为 null
。 我也玩过调用顺序,但无济于事。
任何人都可以阐明这一点或提出解决方法吗?
作为记录,在同一个表单上,我在同一表单上有另一个 ComboBox
,其 SelectedValue
绑定到相同的 BindingSource
。 DataSource
是一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎哟。 在这个问题上浪费了半天时间后,我已经弄清楚了。 这完全是我的错误。
KVPList
设置为KeyValuePair<
short
的
,但数据字段的类型为IList
, string>int
。 本质上,数据绑定将触发,并设置SelectedValue
属性。 然后,DisplayMember
和ValueMember
绑定将触发,并再次检查SelectedValue
。 由于ValueMember
的类型为short
,而不是int
,因此它找不到匹配项,因此将其设置为 null。装箱和拆箱肯定发生了一些有趣的事情,但我现在太累了,无法理解为什么。
我会保留这个问题,以防其他人遇到同样的问题。 很难追踪,因为我希望它要么尝试强制转换,要么抛出异常,而不是默默地返回 null。 毕竟,
short
和int
都是值类型,并且我上次检查的(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 anIList
ofKeyValuePair<
short
,string>
, but the data field is of typeint
. Essentially, the databinding would fire, and set theSelectedValue
property. Then theDisplayMember
andValueMember
bindings would fire, checking theSelectedValue
again. Since theValueMember
is of typeshort
, notint
, 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
andint
are both value types and last I checked(int)10 == (short)10
holds true.