的WinForms数据绑定和外键关系绑定、关系、数据、WinForms

2023-09-04 02:41:22 作者:走过不再回首

我正在开发一个WinForms应用程序(.NET 3.5,无WPF),我希望能够在一个数据绑定DataGridView中显示的外键查找。

I'm developing a WinForms application (.Net 3.5, no WPF) where I want to be able to display foreign key lookups in a databound DataGridView.

关系的排序的一个例子是,我有OrderLines的表。 Orderlines有又一个外键关系,以产品和产品有一个外键关系ProductTypes。

An example of the sort of relationship is that I have a table of OrderLines. Orderlines have a foreign key relationship to Products and Products in turn have a foreign key relationship to ProductTypes.

我想有一个数据绑定的DataGridView每行再presents的订单行,显示该行的产品和producttype。

I'd like to have a databound DataGridView where each row represents an orderline, displaying the line's product and producttype.

用户可以添加或编辑orderlines引导至电网和选择产品,用于从comboBoxColumn订单项 - 这个应该再更新producttype列,示出了producttype所选产品,在同一行中

Users can add or edit orderlines direct to the grid and choose the product for the order line from a comboBoxColumn - this should then update the producttype column, showing the producttype for the selected product, in the same row.

在最接近我到目前为止已经找到了一个不错的选择就是引入一个域对象重新presenting那么订单行DataGridView的绑定到这些orderlines的集合。然后我到暴露的产品和producttype OrderLine的对象添加属性,并提高相关notifypropertychanged事件把一切都处于最新状态。在我的订单行库,我可以再用钢丝了这个订单行对象,然后在我的数据库中的三个表之间的映射。

The closest to a good fit that I've found so far is to introduce a domain object representing an orderline then bind the DataGridView to a collection of these orderlines. I then add properties to the orderline object that expose the product and the producttype, and raise relevant notifypropertychanged events to keep everything up to date. In my orderline repository I can then wire up the mappings between this orderline object and the three tables in my database.

这适用于物联网的数据绑定的一面,但不必手工code所有OR映射在资源库中​​似乎不好。我认为NHibernate的将能够帮助这个线路了,但我在努力通过所有的外键的映射 - 他们似乎工作正常(的外键查找一个订单行的产品将基于外键的正确的产品对象),直​​到我试着做了绑定,我不能让数据绑定ID列来更新我的产品或producttype对象。

This works for the databinding side of things, but having to hand code all that OR-mapping in the repository seems bad. I thought nHibernate would be able to help with this wiring up but am struggling with the mappings through all the foreign keys - they seem to work ok (the foreignkey lookup for an orderline's product creates the correct product object based on the foreign key) until I try to do the databinding, I can't get the databound id columns to update my product or producttype objects.

即使是在正确的球场我一般的做法?如果是,什么是一个很好的解决映射问题?

Is my general approach even in the right ballpark? If it is, what is a good solution to the mapping problem?

或者,有没有绑定行包括我还没有考虑外键查找一个更好的解决方案?

Or, is there a better solution to databinding rows including foreign key lookups that I haven't even considered?

推荐答案

我想你遇到的问题是,当你被绑定到一个网格,它​​是不足以支持INotifyPropertyChanged的,但你要火的ListChanged在 IBindingList的事件的实施,并确保你覆盖并返回true SupportsChangeNotification 财产。如果不返回true为此,格不会看它知道如果数据发生了变化。

I think the problem you're having is that when you are binding to a grid, it is not enough to support INotifyPropertyChanged, but you have to fire the ListChanged events in your IBindingList implementation and make sure that you override and return true for the SupportsChangeNotification property. If you don't return true for this, the grid won't look for it to know if the data has changed.

在.NET 2.0 +,您可以使用的BindingList 类,这会照顾大多数的污秽(只是别忘了重写,并返回true SupportsChangeNotification属性)。

In .NET 2.0+, you can create a generic collection using the BindingList class, this will take care of most of the nastiness (just don't forget to override and return true for the SupportsChangeNotification property).

如果您使用数据绑定类有一个属性,它是一个集合(如IBindingList的或的BindingList),那么你就可以绑定外键电网直接财产。当您配置在窗体设计器的绑定,只需选择集合属性作为数据源的网格。它应该只是工作。只有偷偷摸摸的部分是确保你处理空或空集的正确方法。

If the class you use for data binding has a property that is a collection (such as IBindingList or BindingList), then you can bind the foreign key grid to that property directly. When you configure the bindings in the Forms designer, just select the collection property as the data source for the grid. It should "just work". The only sneaky part is making sure that you handle empty or null collections the right way.