views:

33

answers:

2

I'd like to fill a multi-column System.Windows.Forms.ListView with the items I've stored in a separate System.Collections.Generics.List<CustomClass>. I would like to avoid to store the data twice, once in the List<CustomClass> and once as a string in ListViewItem. Is there a way to make ListViewItem use some callback function to obtain the text to put in its columns from the Tag property, instead of using its Text property?

Update: I've tried the OnDrawSubItem solution and it worked, but I experienced strange window invalidation problems (the ListView was always redrawn correctly, but the other windows in the dialog weren't). However, I found what I was looking for: ListView in "virtual mode". It allows to fill the ListView with data from another source, which is automatically updated when the source is modified.

+1  A: 

Not sure I understand what it is you mean, if your List<> is a of type string you can just add the strings to the list and they will display anyway.

If your list is a class then you could just override the .ToString method.

Edit

Based on your comment. You would need to do a custom draw on the cells. I think the OnDrawSubItem event is the one you want to handle. Then based on column id (which you will get from the DrawListViewSubItemEventArgs you can set the text.

James
Thanks for your answer. I've updated by question, because I want to use a multi-column ListView.
Dimitri C.
+1  A: 

No, there is no ListViewItem callback function like this. You can make CustomListView, and override OnDraw... methods.

Andrew