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.
Not sure I understand what it is you mean, if your
List<>is a of typestringyou 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
.ToStringmethod.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
DrawListViewSubItemEventArgsyou can set the text.