Click here to Skip to main content
15,893,190 members
Articles / Desktop Programming / Windows Forms

Extending the ListViewItem

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
14 May 2009CPOL6 min read 51K   929   28  
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)


Written By
Software Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions