views:

4601

answers:

4

I have a datasource that I want to bind to a listview that has multiple columns. How do I bind my datasource to that listview

Here is some pseudo code that doesn't work to help illustrate what I am trying to do:

MyDataTable dt = GetDataSource();
ListView1.DataBindings.Add("Column1.Text", dt, "MyDBCol1");
ListView1.DataBindings.Add("Column2.Text", dt, "MyDBCol2");

-- edit --

Sorry, I forgot to mention it was winforms.

A: 

Check out this reference on binding datasource to listview. Is that what you were looking for?

Handruin
This link talks about ASP.NET ListView and not the Winforms ListView.
derSteve
A: 

Can you please clarify whether this is winforms vs webforms? Both have a ListView. From the DataBindings.Add I'm assuming winforms.

Would DataGridView be a pragmatic option? This will support multi-column binding out of the box?

Marc Gravell
+1  A: 

It seems there is a lacuna in functionality of WinForms ListView control (thought it's possible to databind new WPF ListBox).

This article by Nick Karnik describes how to add databinding capability to custom control inherited from WinForms ListView - Data binding a ListView.

Alexander Prokofyev
A: 

Listview has no Datasource, Items must be added manually.

I would suggest to use the DatagridView instead of the Listview.
Particularly if you have a lot of items.

Peter Gfader