Click here to Skip to main content
12,395,871 members (63,528 online)
Click here to Skip to main content

Stats

31.4K views
693 downloads
26 bookmarked
Posted

Extending the ListViewItem

, 14 May 2009 CPOL
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

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 14 May 2009
Article Copyright 2009 by flavius.stoichescu
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid