I want to maintain both ID and Object Type in my ListView. I’m trying to do this:
lstView.Items.Insert(MyObject);
// can't do this, because it takes only Int and String
In my case, the ID is int, so that part is ok. But how to insert an object type and retrieve it in the Item_Selection changed event?
A
ListViewcannot add or insert an object directly like aListBoxorComboBox, but instead you need to create aListViewItemand set itsTagproperty.The Tag property from Msdn
Example code:
When you need to get your object back from the
ListView, you can cast theTagproperty.Usually its easier to wrap the functionality into a helper method.
And you can do:
A
ListViewdoesn’t support aDataSourceproperty like a lot of controls, so if you wish to data bind you will need to implement something a bit more custom.