如何将数据字段值绑定到组合框索引?

发布于 2024-09-24 17:10:09 字数 436 浏览 3 评论 0原文

这可能已经被问过,但我似乎找不到这个具体问题,所以这里...

我有一个 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 技术交流群。

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

发布评论

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

评论(2

合约呢 2024-10-01 17:10:10

我假设您的表单上有一个 BindingSource 来绑定到数据。您可以按如下方式绑定 ComboBoxSelectedIndex 属性:

comboBox.DataBindings.Add("SelectedIndex", bindingSource, "PropertyInTheDataSource");

I assume you have a BindingSource on your form to bind to the data. You can bind the SelectedIndex property of the ComboBox as follows:

comboBox.DataBindings.Add("SelectedIndex", bindingSource, "PropertyInTheDataSource");
最终幸福 2024-10-01 17:10:10

实际上我能够将它绑定到自定义对象。对于这样一个简单的任务来说,工作量有点太大了。但您可以完全控制显示/值对。不管怎样,我想我会分享,你决定:
创建一个包含 2 个字段的新类(例如 CustomItem):

Public int Value{get;set;}  
public string Title {get;set;}  

然后在您的表单中:

var item1 = new CustomItem() { Title = "A", Value = 10 };
        var item2 = new CustomItem() { Title = "B", Value = 20 };
        var item3 = new CustomItem() { Title = "C", Value = 30 };
        var lst = new List<CustomItem>();
        lst.Add(item1);
        lst.Add(item2);
        lst.Add(item3);
        comboBox1.DataSource = lst;
        comboBox1.DisplayMember = "Title";
        comboBox1.ValueMember = "Value";

现在您有一个数据绑定组合框,以防您的表单中没有 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:

Public int Value{get;set;}  
public string Title {get;set;}  

Then in you form:

var item1 = new CustomItem() { Title = "A", Value = 10 };
        var item2 = new CustomItem() { Title = "B", Value = 20 };
        var item3 = new CustomItem() { Title = "C", Value = 30 };
        var lst = new List<CustomItem>();
        lst.Add(item1);
        lst.Add(item2);
        lst.Add(item3);
        comboBox1.DataSource = lst;
        comboBox1.DisplayMember = "Title";
        comboBox1.ValueMember = "Value";

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.

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