C#: Tagging an object to a listview item and retrieving it

trevor1940
trevor1940 used Ask the Experts™
on
This page suggests the tag can be referenced  to any object

Can someone explain how to retrieve the properties of that object on selecting the listview item?

I get
Error      CS0029      Cannot implicitly convert type 'System.Windows.Forms.ListViewItem' to 'ClassName.Episode'      TVDB

sudo Code

//  build listview have accessed / created episode
............

                                    ListViewItem item = new ListViewItem();
                                    item.Text = fName;
                                    item.Tag = episode;
                                    item.SubItems.Add(newName);
                                    listView1.Items.Add(item);

Open in new window

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listView1.SelectedIndices[0];
            string fName = listView1.Items[i].Text;

            Episode episode = listView1.Items[i];  // errors here


         }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Freelance programmer / Consultant
Commented:
Change
Episode episode = listView1.Items[i];

Open in new window

to
Episode episode = listView1.Items[i].Tag;

Open in new window

 

ps.  You might need to cast the object specifically
Episode episode = (Episode)(listView1.Items[i].Tag);

Open in new window

Author

Commented:
Thanx Andy

I did have to cast the object

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial