I'm trying to add items to a ListView
control. I wish to add the items with a text value (which is shown) and a hidden key value that it has when it is selected.
I've tried the following code:
string flows_path = "C:\\temp\\Failed Electricity flows\\";
List<ListViewItem> flows_loaded = new List<ListViewItem>();
foreach (string s in Directory.GetFiles(flows_path, "*.rcv").Select(Path.GetFileName))
{
ListViewItem new_item = new ListViewItem(s, 1);
ListViewItem.ad
// Add the flow names to the list
flows_loaded.Add(new_item);
}
But it tells me that ListViewItem
doesn't have an overload of (string, int)
and it doesn't appear to have a 'value', 'text' or 'key' value that I can set.
ListViewItem("My Item")
works, but I don't know how to implement a key for each item.
You can store an additional value associated with a ListViewItem by storing it in the Tag
property.
ListViewItem new_item = new ListViewItem(s);
new_item.Tag = my_key_value;
ETA: Please remember that the Tag
property is of type object
, so in some cases you may need to explicitly cast values to the proper type when retrieving the value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With