Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7908181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:48:46+00:00 2026-06-03T11:48:46+00:00

I want to maintain both ID and Object Type in my ListView. I’m trying

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:48:49+00:00Added an answer on June 3, 2026 at 11:48 am

    A ListView cannot add or insert an object directly like a ListBox or ComboBox, but instead you need to create a ListViewItem and set its Tag property.

    The Tag property from Msdn

    An Object that contains data about the control. The default is null.

    Any type derived from the Object class can be assigned to this
    property. If the Tag property is set through the Windows Forms
    designer, only text can be assigned. A common use for the Tag
    property is to store data that is closely associated with the control.
    For example, if you have a control that displays information about a
    customer, you might store a DataSet that contains the customer’s order
    history in that control’s Tag property so the data can be accessed
    quickly.

    Example code:

    MyObject myObj = new MyObject();
    
    ListViewItem item = new ListViewItem();
    item.Text = myObj.ToString(); // Or whatever display text you need
    item.Tag = myObj;
    
    // Setup other things like SubItems, Font, ...
    
    listView.Items.Add(item);
    

    When you need to get your object back from the ListView, you can cast the Tag property.

    private void OnListViewItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
        MyObject myObj = (MyObject)e.Item.Tag;
        int id = myObj.Id;
    
        // Can access other MyObject Members
    }
    

    Usually its easier to wrap the functionality into a helper method.

    public static void CreateListViewItem(ListView listView, MyObject obj) {
        ListViewItem item = new ListViewItem();
        item.Tag = obj;
    
        // Other requirements as needed
    
        listView.Items.Add(item);
    }
    

    And you can do:

    CreateListViewItem(listView, obj);
    

    A ListView doesn’t support a DataSource property like a lot of controls, so if you wish to data bind you will need to implement something a bit more custom.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page in which I want to maintain the value of object
I want to maintain state in my Perl web app. How can I do
I want to create and maintain a relatively simple relational database in my java
I want my window to always maintain a certain ratio of let's say 1.33333333.
want to know why String behaves like value type while using ==. String s1
want to have a Hyperlink-Button in a gridView in which I can display a
I have developed an Android app which I want to be available both as
I just want to ask, does it make sense to use both a PHP
I maintain a Python program that provides advice on certain topics. It does this
So I want to know if this is a good idea or a bad

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.