|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.