James Jeffery
James Jeffery

Reputation: 12599

ListView WinForms Bind to ObservableCollection<T>

Is it possible to bind an ObservableCollection to a ListView in Winforms? All the examples I'm seeing are for WPF but I can't find anything for Winforms.

Ideally I'd like to bind the following:

ObservableCollection<List> accounts = new ObservableCollection<List>();
accounts.add("someuser");
accounts.add("someotheruser");

... to the ListView. Using an ObservableCollection (as can be done in WPF) so that any changes to the ObservableCollection will cause the ListView to update.

Here's what I've tried - taking a stab in the dark ...

groups.Add(new Group
{
    title = "Mathematics Group",
    id = "034234",
    members = "54"
});

listViewGroups.CheckBoxes = true;
listViewGroups.Columns.Add("checkbox", "");
listViewGroups.Columns.Add("groupid", "Group ID");
listViewGroups.Columns.Add("groupname", "Group Name");

listViewGroups.DataBindings.Add("groupname", groups, "title");

Is it possible?

Upvotes: 2

Views: 3527

Answers (2)

Grammarian
Grammarian

Reputation: 6882

Very late answer, but for future reference.

ListView in .NET WinForms does not support data binding.

ObjectListView -- an open source enhancement wrapper around a ListView -- does.

Upvotes: 2

Mayank
Mayank

Reputation: 8852

It's possible but you may need to use some external library that provides INotifyPropertyChanged interface. You can use PostSharp for that.

Upvotes: 0

Related Questions