如何将数据字段值绑定到组合框索引?
这可能已经被问过,但我似乎找不到这个具体问题,所以这里...
我有一个 C# 表单,其中每个文本框都绑定到一行中的一个字段。可以通过底部的一些按钮循环显示行,但一次显示的所有数据都来自一行。当用户单击“更新”时,所做的任何更改都会更新回数据库。
一个字段(类)是一个枚举(0,1,2),其中仅将值存储在数据库中,但对用户来说没有多大意义。用户。我被要求让这一点对用户来说更加明显,所以我决定使用下拉式组合框。由于数据库没有任何对值含义的引用,我决定使用 DataBindings 而不是 DataSource,这样我就可以使用索引作为数据绑定,但似乎 SelectedItem 或 Value 不是执行此操作的方法。
这是我的目标:
数据库中存在 1,因此在组合框中选择“B”。
用户选择“C”并更新数据库,2 现在存储在数据库中。
对于我需要什么才能使其正常工作有什么想法吗?
This may have been already asked but I can't seem to find this specific question, so here goes...
I have a form in C# where every textbox is bound to a field in a row. Rows can be cycled through by some buttons on the bottom but all the data displayed at a time in the from is from one row. Any changes that are made get updated back to the database when the user clicks "update"
One field (class) is an enumeration (0,1,2) where only the value is stored in the database, but doesn't mean much to the user. I was asked to make this more obvious to the user, so I decided to go with a dropdown style combo box. Since the database didn't have any reference to what the values meant, I decided to use the DataBindings instead of DataSource so I could just use the index as the data bind, but it seems that SelectedItem or Value are not the way to do this.
Here is my goal:
1 exists in database, so "B" is selected in combo box.
User selects "C" and updates the database, 2 is now stored in the database.
Any thoughts on what I need to get this working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您的表单上有一个
BindingSource
来绑定到数据。您可以按如下方式绑定ComboBox
的SelectedIndex
属性:I assume you have a
BindingSource
on your form to bind to the data. You can bind theSelectedIndex
property of theComboBox
as follows:实际上我能够将它绑定到自定义对象。对于这样一个简单的任务来说,工作量有点太大了。但您可以完全控制显示/值对。不管怎样,我想我会分享,你决定:
创建一个包含 2 个字段的新类(例如 CustomItem):
然后在您的表单中:
现在您有一个数据绑定组合框,以防您的表单中没有 BndingSource。
只需记住将类的标题和值定义为属性,否则它将无法工作。
Actually I was able to bind it to a custom Object. It's a little too much work for such a simple task. But you have complete control on Display/Value pairs. Anyway, I thought I'd share and you decide:
Create a new class (say CustomItem) with 2 fields:
Then in you form:
Now You have a databound combobox in case you don't have BndingSource in your form.
Just remember to define your class's Title and Value as properties otherwise it wouldn't work.