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
- DataGridView绑定复杂对象
- win8_绑定复杂对象
- WINJS:绑定复杂对象
- 复杂绑定
- 如何:访问绑定到 Windows 窗体 DataGridView 行的对象
- DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法
- WinForm程序用使用List对象绑定DataGridView数据源
- 笔记:vb.net的datagridview绑定对象作为数据源
- DataGridView绑定list对象,出现索引 0 没有值
- WinForm程序用使用List对象绑定DataGridView数据源
- WinForm程序用使用List对象绑定DataGridView数据源
- XFire绑定Aegis自定义友好参数(复杂对象)
- SpringMVC数据绑定全面示例(复杂对象,数组等)
- SpringMVC数据绑定全面示例(复杂对象,数组等)
- springMVC4(12)复杂对象和集合类型入参绑定
- SpringMVC数据绑定全面示例(复杂对象,数组等)
- dataGridView绑定Dictionary |Dictionary绑定到DataGridView
- WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)
- HTTP连接不能及时关闭导致连接堆积的问题
- 查看硬件信息命令
- Linux USB 鼠标驱动程序解析
- 客户区和非客户区指的什么?窗口客户区和视图客户区的区别!
- mtd 设备 关键数据 创建过程
- DataGridView绑定复杂对象
- 现代五子棋
- ewebeditor不登陆后台也有机会拿shell
- WinCE USB驱动CDevice::EnterOperationalState函数相关
- 初识AutoHotKey
- 强大的计算机工作面试笔试题目...
- 使用Application变量
- fodero9 nfs mini6410
- 一步步学汇编(六)内存寻址方式