简体   繁体   English

将ListView绑定到Winforms中的列表/数据库表的最佳方法

[英]Best way to bind a ListView to a list / database table in winforms

I need to bind several properties from an Invoice table in a database to a ListView control. 我需要将数据库中的发票表中的几个属性绑定到ListView控件。

For example, a row in my Invoices table might look like this: 例如,“发票”表中的一行可能如下所示:

| InvoiceId | VendorID | InvoiceNumber | InvoiceDate | InvoiceTotal |
|    100    |   10     |    909090     | 2008-09-18  |  23.95       |

And I want the properties "InvoiceDate" and "InvoiceTotal" to display in a ListView control, and have the user press a button to see a new form showing a more extensive set of details for this table row. 我希望属性“ InvoiceDate”和“ InvoiceTotal”显示在ListView控件中,并让用户按下按钮以查看新表格,该表格显示了该表行的更多详细信息。

This is something I've done previously quite easily with databinding in Windows Phone / WPF, but what's the best way to do this in winforms? 我以前在Windows Phone / WPF中使用数据绑定非常轻松地完成了此操作,但是在winforms中执行此操作的最佳方法是什么?

My current solution is to add new ListViewItem(s) to the ListView control in a loop, where each ListViewItem's subitems has the properties from the Invoice row that I want it to display. 我当前的解决方案是在循环中将新的ListViewItem添加到ListView控件中,其中每个ListViewItem的子项都具有要显示的发票行中的属性。 This displays them just fine, but it means the ListView control doesn't really keep track of the Invoice items, so I can't easily pass the correct Invoice item (or Invoice primary key) to the new form when the user presses the button. 这样就可以很好地显示它们,但这意味着ListView控件并不能真正跟踪发票项,因此当用户按下按钮时,我无法轻松地将正确的发票项(或发票主键)传递给新表单。

Any suggestions? 有什么建议么?

ObjectListView (an open source wrapper around a .NET ListView) adds data binding (plus lots of other neat features) to the .NET ListView. ObjectListView (围绕.NET ListView的开源包装器)将数据绑定(以及许多其他简洁功能)添加到.NET ListView。 See this recipe for data binding. 有关数据绑定,请参见此食谱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM