Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Extending the ListViewItem

, 14 May 2009
How to extend the ListViewItem to hold an object for a row, and map it to the columns in the ListView control.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListViewExtendedItem
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
            colID.Name = "colID";
            colName.Name = "colName";
            colDate.Name = "colDate";
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            TestClass testObject = new TestClass(1, "Some name", DateTime.Now);
            objectPropGrid.SelectedObject = testObject;
            btnAdd.Enabled = true;
        }

        private void objectList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (objectList.SelectedItems.Count == 1)
            {
                btnUpdate.Enabled = true;
                ListViewExtendedItem item = (ListViewExtendedItem)objectList.SelectedItems[0];
                objectPropGrid.SelectedObject = item.Data;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            btnAdd.Enabled = false;
            ListViewExtendedItem item = new ListViewExtendedItem(objectList, objectPropGrid.SelectedObject);
            objectList.Items.Add(item);
            objectPropGrid.SelectedObject = null;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            btnUpdate.Enabled = false;
            ((ListViewExtendedItem)objectList.SelectedItems[0]).Update(objectPropGrid.SelectedObject);
            objectPropGrid.SelectedObject = null;
            objectList.SelectedItems.Clear();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

flavius.stoichescu
Software Developer
Romania Romania
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 14 May 2009
Article Copyright 2009 by flavius.stoichescu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid