简体   繁体   中英

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.

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.

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?

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. 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.

Any suggestions?

ObjectListView (an open source wrapper around a .NET ListView) adds data binding (plus lots of other neat features) to the .NET ListView. See this recipe for data binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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