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.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace ListViewExtendedItem
{
    public class ListViewExtendedItem : ListViewItem
    {
        private object _data;

        public object Data
        {
            get { return _data; }
        }

        public ListViewExtendedItem(ListView parentListView, object data)
        {
            Update(data, parentListView);
        }

        public void Update(object data)
        {
            if (this.ListView != null)
            {
                Update(data, this.ListView);
            }
            else
            {
                throw new Exception("The item does not contain a refference of a ListViewControl, please use the provided overload of this method and specify a ListView control");
            }
        }

        public void Update(object data, ListView listView)
        {
            this.SubItems.Clear();
            Type typeOfData = data.GetType();
            bool completed_column = false;
            foreach (ColumnHeader column in listView.Columns)
            {
                completed_column = false;
                foreach (PropertyInfo pInfo in typeOfData.GetProperties())
                {
                    foreach (object pAttrib in pInfo.GetCustomAttributes(true))
                    {
                        if (pAttrib.GetType() == typeof(ListViewColumnAttribute))
                        {
                            if (pAttrib.ToString() == column.Name)
                            {
                                if (column.DisplayIndex == 0)
                                {
                                    this.Text = pInfo.GetValue(data, null).ToString();
                                    completed_column = true;
                                    break;
                                }
                                else
                                {
                                    this.SubItems.Add(pInfo.GetValue(data, null).ToString());
                                    completed_column = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (completed_column)
                    {
                        break;
                    }
                }
            }
            _data = data;
        }
    }
}

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
Web02 | 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