Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get id key from List View - C# Windows application

Tags:

c#

.net

listview

I am listing set of table items(only names) in List View. I enabled checkbox property of ListView. I displayed all items as Large icons. I want to attach a key(id) for that items for further processing on that items.. If any idea , please reply

like image 205
Ramin Avatar asked Jan 21 '12 09:01

Ramin


2 Answers

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

like image 91
Hans Passant Avatar answered Sep 27 '22 21:09

Hans Passant


Description

You should use the Tag property for things like that.

Gets or sets the object that contains data about the control.

You can set your id or any other object to the ListItem you want.

Sample

Add the Item

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

Use the index

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
}

More Information

  • MSDN - Control.Tag Property
like image 22
dknaack Avatar answered Sep 27 '22 21:09

dknaack