什么时候值得使用 BindingSource?

发布于 09-05 06:58 字数 1131 浏览 11 评论 0原文

我想我非常了解 BindingSource 类的作用 - 即在数据源和 UI 控件之间提供一个间接层。它实现了 IBindingList 接口,因此还提供了对排序的支持。而且我已经经常使用它,没有太多问题。但我想知道我使用它的频率是否超过了应有的频率。也许一个例子会有所帮助。

假设我的表单上只有一个简单的文本框(使用 WinForms),并且我想将该文本框绑定到返回字符串的类内的一个简单属性。在这种情况下值得使用 BindingSource 吗?

现在假设我的表单上有一个网格,我想将其绑定到数据表。我现在应该使用 BindingSource 吗?

在后一种情况下,我可能不会使用 BindingSource,因为据我所知,DataTable 提供了与 BindingSource 本身相同的功能。当添加、删除行等时,DataTable 将触发正确的事件,以便网格自动更新。

但在第一种情况下,文本框绑定到字符串,我可能会让包含字符串属性的类实现 INotifyPropertyChanged,这样它就可以在字符串更改时触发 PropertyChanged 事件。我将使用 BindingSource,以便它可以侦听这些 PropertyChanged 事件,以便在字符串更改时自动更新文本框。

到目前为止这听起来怎么样?我仍然觉得我的理解上存在差距,导致我看不到全局。到目前为止,这是一个相当模糊的问题,所以我会尝试提出一些更具体的问题 - 理想情况下,答案将引用上面的示例或类似的内容...

(1) 是否值得在上述任一中使用 BindingSource例子?

(2) 开发人员似乎只是“假设”DataTable 类会做正确的事情,在正确的时间触发 PropertyChanged 事件。如何知道数据源是否能够做到这一点?数据源是否应该实现一个特定的接口,以便开发人员能够承担这种行为?

(3) 在考虑是否使用 BindingSource 时,绑定到哪个 Control 重要吗?或者仅仅是数据源会影响决策?也许答案是(这看起来很合乎逻辑):Control 需要足够智能来侦听 PropertyChanged 事件,否则需要 BindingSource。那么如何判断控件是否能够做到这一点呢?同样,开发人员是否可以寻找控件必须实现的特定接口?

正是这种混乱导致我在过去总是使用 BindingSource。但我想更好地了解何时使用它,以便我仅在必要时才这样做。

I think I understand well enough what the BindingSource class does - i.e. provide a layer of indirection between a data source and a UI control. It implements the IBindingList interface and therefore also provides support for sorting. And I've used it frequently enough, without too many problems. But I'm wondering if I use it more often than I should. Perhaps an example would help.

Let's say I have just a simple textbox on a form (using WinForms), and I'd like to bind that textbox to a simple property inside a class that returns a string. Is it worth using a BindingSource in this situation?

Now let's say I have a grid on my form, and I'd like to bind it to a DataTable. Should I use a BindingSource now?

In the latter case, I probably would not use a BindingSource, as a DataTable, from what I can gather, provides the same functionality that the BindingSource itself would. The DataTable will fire the the right events when a row is added, deleted, etc so that the grid will automatically update.

But in the first case with the textbox being bound to a string, I would probably have the class that contains the string property implement INotifyPropertyChanged, so that it could fire the PropertyChanged event when the string changes. I would use a BindingSource so that it could listen to these PropertyChanged events so that it could update the textbox automatically when the string changes.

How does this sound so far? I still feel like there's a gap in my understanding that's preventing me from seeing the whole picture. This has been a pretty vague question so far, so I'll try to ask some more specific questions - ideally the answers will reference the above examples or something similar...

(1) Is it worth using a BindingSource in either of the above examples?

(2) It seems that developers just "assume" that the DataTable class will do the right thing, in firing PropertyChanged events at the right time. How does one know if a data source is capable of doing this? Is there a particular interface that a data source should implement in order for developers to be able to assume this behaviour?

(3) Does it matter what Control is being bound to, when considering whether or not to use a BindingSource? Or is it only the data source that should affect the decision? Perhaps the answer is (and this would seem logical enough): the Control needs to be intelligent enough to listen to the PropertyChanged events, otherwise a BindingSource is required. So how does one tell if the Control is capable of doing this? Again, is there a particular interface that developers can look for that the Control must implement?

It is this confusion that has, in the past, led to me always using a BindingSource. But I'd like to understand better exactly when to use one, so that I do so only when necessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

别忘他2024-09-12 06:58:21

很老的问题。奇怪为什么到现在还没人回答。好的,我会尝试分享我的经验。

BindingSource 不仅仅是将控件绑定到集合的一种方法。在使用 WinForms 十多年之后,我最喜欢的 BindingSource 的最佳功能包括:

  1. 绑定(当然!)
  2. 货币管理(我稍后会谈到
  3. BindingSource 可以充当另一个 BindingSource数据源

为了充分理解这些功能,我将在 DataSet 的上下文中解释它们,DataSet 是迄今为止 WinForms 中使用的最常见的数据源类型,尤其是在业务线应用程序中。

货币管理归结为当前记录的概念。 DataTable 只是DataRow 的集合,即DataTable 中没有当前 记录的概念。 DataView 的情况也是如此(附带说明,您不能直接绑定到 DataTable;当您这样做时,它实际上绑定到 DefaultViewDataTable 的 code> 属性,它是一个 DataView,您也可以创建自己的 DataView)。

在主/详细类型 UI 的情况下,货币管理确实非常方便。假设您在左窗格(主窗口)中有一个学生列表框,在右窗格中有几个文本框、组合框、复选框等,以及所选学生课程的网格(详细信息)。在您的数据集中,您有两个名为 StudentCourses 的数据表。为了简单起见,我在这里避免使用动名词(Student_Course)。 Course 表有一个外键 StudentID。以下是您在此处设置绑定的方法(请注意我上面列出的所有 3 个功能如何在下面的设置中使用):

  1. 将两个 BindingSource 控件添加到您的表单,名为 bsStudent 和 <代码>bsCourses。
  2. bsStudentDataSource设置为Student DataTable。
  3. bsCoursesDataSource设置为bsStudent!
  4. DataMember 属性中,您将看到两个表之间的 DataSet 中存在的关系的名称。选择它!
  5. 将各个原子控件的绑定设置为 bsStudent 的属性。
  6. 设置课程网格 bsCourses 的DataSource

你就完成了。无需编写任何代码(可以这么说),您就成功创建了主从视图。 BindingSource 控件现在将处理学生列表中的当前记录,不仅更新原子控件(文本框、组合框等),还更新课程网格,课程网格将自动更新其内容以显示当前所选学生的课程。

我的朋友,这就是我最喜欢的 BindingSource 的角色(以及其他好的功能,如排序、过滤等)。如果在控件和数据存储之间不涉及 BindingSource,您就没有当前记录的概念,因此必须手动管理保持所有 UI 同步。

Pretty old question. Wonder why anyone hasn't answered it till now. OK, I'll try and share things from my experience.

A BindingSource is more than just a way to bind controls to collections. After having worked in WinForms for over a decade, the best features of a BindingSource that I like the most include:

  1. Binding (of course!)
  2. Currency management (I'll come to that in a second)
  3. A BindingSource can act as a data source of another BindingSource.

To fully appreciate these features, I'll explain them in the context of a DataSet, which is by far the most common type of data source used in WinForms, especially in line-of-business apps.

Currency management boils down to the concept of current record. A DataTable is just a collection of DataRows, i.e. there is no concept of current record in DataTables. Same is the case of DataView (on a side note, you cannot directly bind to a DataTable; when you do that, it actually binds to the DefaultView property of that DataTable, which is a DataView. You could create your own DataView too).

Currency management really proves handy in case of Master/Detail kind of UI. So let's say you have a ListBox of Students in the left pane (Master), and several TextBoxes, ComboBoxes, CheckBoxes etc. in the right pane, with a grid of selected student's courses (Detail). In your DataSet, you have two DataTables named Student and Courses. For the sake of simplicity, I'm avoiding a gerund (Student_Course) here. The Course table has a foreign key StudentID. Here's how you setup binding here (note how all the 3 features I listed above are used in the setup below):

  1. Add two BindingSource controls to your form, named bsStudent and bsCourses.
  2. Set DataSource of bsStudent to Student DataTable.
  3. Set DataSource of bsCourses to bsStudent!
  4. In the DataMember property, you'll see the name of the relation that exists in the DataSet between our two tables. Select it!
  5. Set the binding of individual atomic controls to bsStudent's properties.
  6. Set the DataSource of courses grid bsCourses.

And you're done. Without writing a single line of code (so to speak), you have successfully created a master-details view. The BindingSource control will now take care of the current record in Students list and update not only the atomic controls (TextBoxes, ComboBoxes etc.), but also the courses grid, which will automatically update its contents to show the courses of currently selected student.

This, my friend, is the role of BindingSource (among other nice things like sorting, filtering etc.) which I like the most. Without involving a BindingSource in-between your controls and the data store, you'd not have the concept of current record and therefore would manually have to manage keeping all the UI in sync.

旧竹2024-09-12 06:58:21

您好,我对这个问题也有些困惑。
当我使用数据表时,它们实现了所有接口。
不过我总是使用绑定源只是为了确定..:)

有一些争论为什么我可以想到

  1. 同一记录集上的多个视图。 (即 2 个具有不同排序顺序/过滤器的网格)
  2. 过滤、排序,但不更改记录本身的排序顺序(过滤器/排序)
  3. 出于性能原因可以暂时禁用绑定。 (当表中有大更新时,不要监听所有 IXXChanged 事件)
  4. 如果没有绑定源,IErrorprovider 永远不会为我工作,但这可能是我的错。

Hi I also have some confusion about the subject.
When I use datatables those implement all the interfaces.
However I always use the bindingsource just to be sure.. :)

There are some arguments why that I can think of

  1. Multiple views on the same recordset. (ie. 2 grids with diffent sort orders/filters)
  2. Filtering,Sorting while not changing the sort order of the records themselves (Filter/Sort)
  3. Ability to disable binding for a while for performance reasons. (when there are big updates in the table, don'y listen to all the IXXChanged events)
  4. IErrorprovider never worked for me without a bindingsource, however this could be my fault.
乖乖2024-09-12 06:58:21

我还想补充一点,使用 BindingSource,您可以绑定到业务对象,该对象可以实现 INotifyPropertyChanged 事件,因此如果数据发生更改(无论是您的代码还是其他人的代码),您的 UI 可以自动反映更新。

I also would like to add, using BindingSource, you can bind to a business object, which can implement INotifyPropertyChanged event, so if data is changed (whether by your code or someone else's code), your UI can automatically reflect the updates.

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