























public class MyTable : List<MyRow>, ITypedList
{
public MyTable()
{
for (int i = 0; i < 5; i++)
{
MyRow ht = new MyRow();
ht.Table = this;
ht.Add("Name", "name" + i);
ht.Add("Age", i + 10);
ht.Add("Address", "add:" + i);
this.Add(ht);
}
}#region ITypedList Memberspublic PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
{
List<MyPropertyDescriptor> list = new List<MyPropertyDescriptor>();
list.Add(new MyPropertyDescriptor("Name", typeof(string)));
list.Add(new MyPropertyDescriptor("Age", typeof(int)));
list.Add(new MyPropertyDescriptor("Address", typeof(string)));
return new PropertyDescriptorCollection(list.ToArray());
}public string GetListName(PropertyDescriptor[] listAccessors)
{
return string.Empty;
}#endregion
}public class MyRow : Hashtable, ICustomTypeDescriptor
{
public MyTable Table { get; set; }#region ICustomTypeDescriptor Memberspublic System.ComponentModel.AttributeCollection GetAttributes()
{
return new System.ComponentModel.AttributeCollection(null);
}public string GetClassName()
{
return null;
}public string GetComponentName()
{
return null;
}public TypeConverter GetConverter()
{
return null;
}public EventDescriptor GetDefaultEvent()
{
return null;
}public PropertyDescriptor GetDefaultProperty()
{
return null;
}public object GetEditor(Type editorBaseType)
{
return null;
}public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return new EventDescriptorCollection(null);
}public EventDescriptorCollection GetEvents()
{
return new EventDescriptorCollection(null);
}public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return this.Table.GetItemProperties(null);
}public PropertyDescriptorCollection GetProperties()
{
return ((ICustomTypeDescriptor)this).GetProperties(null);
}public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}#endregion
}public class MyPropertyDescriptor : PropertyDescriptor
{
private string _field;
private Type _propertyType;public MyPropertyDescriptor(string name, Type propertyType)
: base(name, null)
{
_field = name;
_propertyType = propertyType;
}public override bool CanResetValue(object component)
{
return false;
}public override bool Equals(object other)
{
if (other is MyPropertyDescriptor)
{
MyPropertyDescriptor descriptor = (MyPropertyDescriptor)other;
return (descriptor._field == this._field);
}
return false;
}public override int GetHashCode()
{
return _field.GetHashCode();
}public override object GetValue(object component)
{
MyRow view = (MyRow)component;
return view[_field];
}public override void ResetValue(object component)
{
((MyRow)component)[_field] = null;
}public override void SetValue(object component, object value)
{
((MyRow)component)[_field] = value;
this.OnValueChanged(component, EventArgs.Empty);
}public override bool ShouldSerializeValue(object component)
{
return false;
}public override Type ComponentType
{
get
{
return typeof(MyRow);
}
}public override bool IsBrowsable
{
get
{
return base.IsBrowsable;
}
}public override bool IsReadOnly
{
get
{
return true;
}
}public override Type PropertyType
{
get
{
return _propertyType;
}
}
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。