views:

664

answers:

2

Hello there.

The following code adds a new item, and a new group with the text "Default". If i keep clicking the button, it will just keep adding new items to that particular group.

ListViewItem item = new ListViewItem("");
item.SubItems.Add("");
csslistview.Items.Add(item);

What i'm trying to do, is to add a new group and fill it with one empty item, aswell as one empty subitem. And when i click the button again, i want it to create a new group, and do the same thing. I have a textbox were the user has to fill in the name of the group, so there wont be any groups with the same name (hopefully).

The following code, i think, creates a new group:

ListViewGroup group = new ListViewGroup(newGroupName);
group.Items.Add(newGroupName);
csslistview.Groups.Add(group);

but as empty groups aren't showed, i can't really verify that it actually creates new groups.

Well, thanks in advance. -Nike

+1  A: 

UPDATED

Ok, I (likely) got it.

You don't know how to link items to group, do you ?

// create a group
ListViewGroup group = new ListViewGroup(newGroupName);
// add group to listview
csslistview.Groups.Add(group);
// create an item
ListViewItem item = new ListViewItem("");
item.SubItems.Add("");
// add item to listview
csslistview.Items.Add(item);
// force item to belong to a group
item.Group = group;
digEmAll
Looks pretty good to me but unfortunately, nothing happens, at all. :( If i change the last line, where i add the group to the listview, to this: csslistview.Items.Add(item); it adds the item to a new group called "Default", as before. But that's not really what i want...
Nike
OK, check the answer update
digEmAll
Still, no changes. :/
Nike
Ignore the above comment, it works wonderfully now! Thanks a bunch! :)
Nike
A: 

If you are doing any work with a ListView, especially playing with the groups, you can save yourself a lot of pain by using ObjectListView. It's a sophisticated wrapper around a .NET WinForms ListView -- it has solutions for all the problems you are going to find when using a ListView, in addition to making it much easier to use.

For example, creating the following grouped listview, was done by adding one statement to an existing ObjectListView:

alt text

Disclaimer: I am the author of this project -- and I am naturally biased. For other people's opinions, see here.

Grammarian
I see that the ObjectListView is licensed under the GNU GPLv3. Does this allow for usage in a commercial project (not being sold but customer is paying for development)? Or is it viral in that the entire program needs to be open-sourced?
John M
GPL is always viral, but you can buy a (cheap) license that allows you to use it in closed source apps.
Grammarian