How to get an identifier key from a list view - C # Windows application

I list a set of table elements (names only) in a list view. I have included the checkbox property ListView. I displayed all the elements as large icons. I want to add a key (id) for these elements to further process these elements. If there is any idea, answer

+5
source share
2 answers

Use the ListViewItem.Name property. Badly named, this is actually the key. The one you can pass to ListView.Items.IndexOfKey () or ListView.Items ["key"].

+8
source

Description

You should use a property Tagfor such things.

, .

id object ListItem, .

ListViewItem myItem = new ListViewItem();
myItem.Tag = 1; // or any other object
listView1.Items.Add(myItem);

private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
    ListViewItem myItem = e.Item;
    int index = (int) myItem.Tag; // cast to your object type, int in this case
    // use the index
}

+3

All Articles