DataGridView绑定复杂对象

来源:互联网 发布:淘宝买正版实体游戏 编辑:程序博客网 时间:2024/04/30 08:30

 数据对象:

 

[TypeDescriptionProvider(typeof(MyTypeDescriptionProvider))]

class Person
{
         private string id;
         private string name;
         private Address homeAddr;

         public string ID
         {
                   get { return id;}
                   set { id = value;}
         }

         public string Name
         {
                   get { return name;}
                   set { name = value;}
         }

         public Address HomeAddr
         {
                   get { return homeAddr;}
                   set { homeAddr = value;}
         }       
}

 

class Address
{

         private string cityname;
         private string postcode;
         public string CityName
         {
                   get { return cityname;}
                   set { cityname = value;}
         }

         public string PostCode
         {
                   get { return postcode;}
                   set { postcode = value;}
         }
}

 

 扩展

 

public class SubPropertyDescriptor : PropertyDescriptor
{
        private PropertyDescriptor _subPD;
        private PropertyDescriptor _parentPD;

        public SubPropertyDescriptor(PropertyDescriptor parentPD,PropertyDescriptor subPD,string pdname)
            : base(pdname,null)
        {          
            _subPD = subPD;
            _parentPD = parentPD;
        }

        public override bool IsReadOnly { get { return false; } }
        public override void ResetValue(object component) { }
        public override bool CanResetValue(object component) { return false; }
        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get { return _parentPD.ComponentType; }
        }

        public override Type PropertyType { get { return _subPD.PropertyType; } }

        public override object GetValue(object component)
        {
           return _subPD.GetValue(_parentPD.GetValue(component));
        }

        public override void SetValue(object component, object value)
        {           
            _subPD.SetValue(_parentPD.GetValue(component), value);
            OnValueChanged(component, EventArgs.Empty);
        }
}


public class MyCustomTypeDescriptor : CustomTypeDescriptor
{      
        public MyCustomTypeDescriptor(ICustomTypeDescriptor parent)
            : base(parent)
        {
        }

        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {

            PropertyDescriptorCollection cols = base.GetProperties(attributes);

            PropertyDescriptor addressPD = cols["HomeAddr"];
            PropertyDescriptorCollection homeAddr_child = addressPD.GetChildProperties();          

            PropertyDescriptor[] array = new PropertyDescriptor[cols.Count + 2];
            cols.CopyTo(array, 0);

            array[cols.Count] = new SubPropertyDescriptor(addressPD,homeAddr_child["CityName"],"HomeAddr_CityName");
            array[cols.Count + 1] = new SubPropertyDescriptor(addressPD, homeAddr_child["PostCode"], "HomeAddr_PostCode");

            PropertyDescriptorCollection newcols = new PropertyDescriptorCollection(array);

            return newcols;        
        }    
  }


public class MyTypeDescriptionProvider : TypeDescriptionProvider
{
        private ICustomTypeDescriptor td;

        public MyTypeDescriptionProvider()
           : this(TypeDescriptor.GetProvider(typeof(Person)))
        {           
        }

        public MyTypeDescriptionProvider(TypeDescriptionProvider parent)
            : base(parent)
        {
        }

        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
        {
            if (td == null)
            {
                td = base.GetTypeDescriptor(objectType, instance);
                td = new MyCustomTypeDescriptor(td);
            }
            return td;
        }       
}

 

最后,绑定DataGridView 

 

 IList<Person> list = GetPersons();

 this.dataGridView.DataSource = list;

dataGridView.Columns[2].DataPropertyName = "HomeAddr_CityName";
dataGridView.Columns[3].DataPropertyName = "HomeAddr_PostCode";

 

 

 http://blogs.msdn.com/b/msdnts/archive/2007/01/19/how-to-bind-a-datagridview-column-to-a-second-level-property-of-a-data-source.aspx

 

 

 

原创粉丝点击