- C# code
public class User { public User() { } public User(int vAge,string vName) { _Age=vAge; _Name=vName; } private int _Age; private string _Name; public int Age { set{_Age=value;} get{return _Age;} } public string Name { set{_Name=value;} get{return _Name;} } private Address _ad = new Address(); public Address adress { set{_ad =value;} get{return _ad ;} } }public class Address{private string _phone; public string Phone{ get{return _phone;}set{_phone = value;}}}
后来需要在datagridview 里面绑定一个list<User>
我定义了三个column,
name, age, phone。
主要是phone column里面的dataPropertyName 应该写什么?
这种对象里面的属性仍是一个复杂对象怎么处理?
------解决方案--------------------
public User()
{
}
public User(int vAge,string vName)
{
_Age=vAge;
_Name=vName;
}
private int _Age;
private string _Name;
public int Age
{
set{_Age=value;}
get{return _Age;}
}
public string Name
{
set{_Name=value;}
get{return _Name;}
}
private Address _ad = new Address();
public Address adress
{
set{_ad =value;}
get{return _ad ;}
}
public string Phone
{
get{return adress.Phone;}
set{adress.Phone= value;}
}
}
不就可以了吗??????
------解决方案--------------------
public class SubPropertyDescriptor<T> : PropertyDescriptor
{
#region Fields
private PropertyInfo propertyInfo;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
/// <param name="name"></param>
public SubPropertyDescriptor(string name)
: base(name, null)
{
Type t = typeof(T);
this.propertyInfo = null;
string[] subPropertyNames = name.Split('.');
if (subPropertyNames.Length == 1)
{
// a regular property
this.propertyInfo = t.GetProperty(name);
}
else
{
// navigate through the subproperties
for (int i = 0; i < subPropertyNames.Length; ++i)
{
this.propertyInfo = t.GetProperty(subPropertyNames[i]);
t = this.propertyInfo.PropertyType;
}
}
}
#endregion
#region Overriden Methods
/// <summary>
/// <see cref="PropertyDescriptor.ComponentType"/>
/// </summary>
public override Type ComponentType
{
get { return typeof(T); }
}
/// <summary>
/// <see cref="PropertyDescriptor.PropertyType"/>
/// </summary>
public override Type PropertyType
{
get { return this.propertyInfo.PropertyType; }
}
/// <summary>
/// <see cref="PropertyDescriptor.IsReadOnly"/>
/// </summary>
public override bool IsReadOnly