ComboBox或ListBox作为Form的索引

编程入门 行业动态 更新时间:2024-10-20 08:38:41
本文介绍了ComboBox或ListBox作为Form的索引-将其他控件绑定到SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这实际上是一个非常简单且基本的问题,如果这是一个重复的问题(当然一定是这个问题),我深表歉意,但是我找不到任何简单的答案,最终把它扔了。

This is actually a very simple and basic question, and my apologies if this is a repeat question (surely it must be), but I can't find a straightforward answer anywhere and am finally throwing in the towel.

我已经在Visual Studio ’17中创建了Windows Forms应用,并基于SQL Server表创建了数据源,并添加了绑定组合框。到现在为止还挺好。使用数据源/数据集面板中的拖放功能,我在文本框中填充了一些字段并绑定到数据。使用自动添加的数据绑定导航器,我可以在表格中滚动并更新所有控件,因此我知道它们都已正确绑定。

I have created a Windows Forms app in Visual Studio '17, created a data source based upon an SQL server table, and added a bound combobox. So far so good. Using the drag and drop feature from the DataSource/DataSet panel, I populate a few fields in text boxes and bind to data. Using the data binding navigator automatically added, I can scroll through the table and the controls all update, so I know they are all properly bound.

现在,我要完成是在组合框选定值更改时使控件更新。但是他们没有。从我所阅读的内容中,我猜测组合框需要以某种方式提醒数据源当前记录是否需要更新?如果是这样,我如何从组合框中将其更新为键值?也许这是完全错误的?我希望VS可以对绑定控件发挥作用,这样我就不必处理每个组合更改事件,查询数据库然后更新每个控件。

Now, all I want to accomplish is to have the controls update when the combobox selected value changes. But they do not. From what I've read, I'm guessing that somehow the combobox needs to alert that the datasource current record needs updating? If so, how do I get it to update to the key value from the combobox? Or maybe this is completely wrong? My hope was that VS would do its magic for bound controls so that I don't have to handle each combo change event, query the database, and then update each control.

我认为这将是一个简单的任务;鉴于它非常基础,肯定会有直接的解释,但要么讨论的问题更加复杂,要么讨论数据绑定过程和对象的迷宫。这是我惯用的和SQL编码的一种附带冒险,所以我希望只需要一两个简单的事件处理程序即可。

I thought this would be a simple task; given that it's so very basic, surely there would be straight forward explanations, but either problems discussed are more complicated or else a labyrinth of data binding procs and objects are discussed. This is sort of a side venture from my usual and SQL coding, so I am hoping a straight forward event handler or two is all that's needed.

推荐答案

当您想要设置像 ComboBox 或 ListBox 之类的列表控件作为索引时对于数据:

When you want to setup a list control like ComboBox or ListBox to act as an index for data:

  • 将其 DataSource 属性设置为相同的其他控件绑定到的> DataSource 。
  • 设置其 DisplayMember 以在组合框中显示,但是您不需要设置 ValueMember 。
  • 请勿触摸 (数据绑定)。您不需要设置数据绑定。
  • Set its DataSource property to the same DataSource to which other controls are bound.
  • Set its DisplayMember to show in combo box, but you don't need to set ValueMember.
  • Don't touch (DataBindings). You don't need to set up data-binding.

示例

假设您有一个 productsBindingSource ,这些是绑定控件的设置:

Assuming you have a productsBindingSource, these are settings for bound controls:

  • idTextBox →数据绑定: Text 属性绑定到 Id 的code>属性
  • nameTextBox →数据绑定: Name 属性绑定到 productsBindingSource
  • priceTextBox →数据绑定: Price 属性绑定到 Price productsBindingSource
  • 的属性
  • idTextBox → Data Bindings: Text property bound to Id property of productsBindingSource
  • nameTextBox → Data Bindings: Name property bound to Name property of productsBindingSource
  • priceTextBox → Data Bindings: Price property bound to Price property of productsBindingSource

然后有一个 ComboBox 作为索引,这些是 ComboBox 的设置:

Then to have a ComboBox to act as index, these are the settings for ComboBox:

  • DataSource 属性设置为 productsBindingSource
  • DisplayMember 属性设置为 Name
  • 请勿触摸 (数据绑定)。您不需要设置数据绑定。
  • DataSource property set to productsBindingSource
  • DisplayMember property set to Name
  • Don't touch (DataBindings). You don't need to set up data-binding.

然后,当您从 ComboBox ,绑定控件将显示所选项目,绑定导航器也将移动:

Then as a result, when you select an item from ComboBox, bound controls will show selected item and binding navigator will move as well:

更多推荐

ComboBox或ListBox作为Form的索引

本文发布于:2023-11-06 14:15:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   ComboBox   ListBox   Form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!