绑定的组合框不匹配列表对象 [英] Bound combobox not matching up to list objects

查看:35
本文介绍了绑定的组合框不匹配列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm new to C# but not to programming. I've been toying around with VS, becoming more comfortable with the language and got stuck on a combobox bound to an int datatable column of an in-memory dataset.

I worked on a DataGridView and had no problems creating a DataGridViewComboBoxColumn and populating its dropdown list using a custom class comprised of Key:int and Desc:string. ValueMember = Key, DisplayMember = Desc. DataSource is set to a BindingList<> populated with the list objects.  Pick from the combobox and save changes works fine in the grid.

Then I moved to a ComboBox.  Both the grid and the ComboBox have the exact same BindingSource. So yes they point to the same row at the same time.

I bind the ComboBox with

cbCtrl.DataBindings.Add( "Text", myBindingSource, ColDBName );

Without populating the dropdown items of the ComboBox, I see the raw int value show up in the ComboBox.  I change rows using the grid and the ComboBox value changes too.  So it's good.

Then I hook up BindingList<> to provide the dropdown list of items in the ComboBox (the same as the grid's column tied to the same column) and it's trouble.  The raw int value (eg 19) does not get matched to the proper "Key" item in the dropdown list of the ComboBox.  I've tried DropDownStyle = DropDown & DropDownList but no luck with either.  And I've looked for hours trying to find a fix.  There are no events hooked up to the ComboBox.  I exepcted automatic hookup of the raw value to the matching "Key" item in the ComboBox's dropdown list.

Is there something else to hookup?  Frustrating since I expect something minor.

Any help would be GREATLY appreciated.  Thank you.





我的尝试:



我还直接使用对象填充ComboBox,而不是连接BindingList<>到了DataSource但行为是一样的。



What I have tried:

I also directly populated the ComboBox with objects instead of hooking up the BindingList<> to the DataSource but the behavior is the same.

推荐答案

我终于偶然发现了答案:



cbCtrl.DataBindings.Add(SelectedValue,myBindingSource,ColDBName);



这就是我所需要的。我是从一篇好文章中得到的。

详细的数据绑定教程 [ ^ ]
I finally stumbled on the answer:

cbCtrl.DataBindings.Add( "SelectedValue", myBindingSource, ColDBName );

That's all I needed. I got it from a great article
A Detailed Data Binding Tutorial[^]


这篇关于绑定的组合框不匹配列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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