Click here to Skip to main content
15,886,825 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 50.9K   928   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.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)


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