• Build my own DataTable


    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 Members

            
    public 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 { getset; }

            
    #region ICustomTypeDescriptor Members

            
    public 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;
                }
            }
        }
  • 相关阅读:
    ASP.NET Routing Debugger
    浏览器 CSS & JS Hack 手册
    基于vmWare5.5环境的VxWorks系统安装总结
    TFS 迁移到 Git
    关于websocket
    自定义单一模块Model类
    学习 C++的用途
    Navigation Controllers and Table Views(中)
    Mac环境下svn的使用
    减少.NET应用程序内存占用的一则实践
  • 原文地址:https://www.cnblogs.com/shcity/p/1999743.html
Copyright © 2020-2023  润新知