Obversity
Obversity

Reputation: 597

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?

Upvotes: 0

Views: 2719

Answers (1)

Grammarian
Grammarian

Reputation: 6882

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.

Upvotes: 2

Related Questions