datagridview 绑定复杂对象
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;}}}
{
get { return false; }
}
/// <summary>
/// <see cref="PropertyDescriptor.GetValue"/>
/// </summary>
public override object GetValue(object component)
{
object value = component;
foreach (string property in this.Name.Split('.'))
{
value = value.GetType().GetProperty(property).GetValue(value, null);
}
return value;
}
/// <summary>
/// <see cref="PropertyDescriptor.SetValue"/>
/// </summary>
public override void SetValue(object component, object value)
{
Type t = typeof(T);
PropertyInfo subPropertyInfo = null;
string[] subPropertyNames = this.Name.Split('.');
if (subPropertyNames.Length == 1)
{
// a regular property
subPropertyInfo = t.GetProperty(this.Name);
}
else
{
// navigate through the subproperties
for (int i = 0; i < subPropertyNames.Length; ++i)
{
subPropertyInfo = t.GetProperty(subPropertyNames[i]);
t = subPropertyInfo.PropertyType;
if (i < subPropertyNames.Length - 1)
{
component = subPropertyInfo.GetValue(component, null);
}
}
}
subPropertyInfo.SetValue(component, value, null);
}
/// <summary>
/// <see cref="PropertyDescriptor.CanResetValue"/>
/// </summary>
public override bool CanResetValue(object component)
{
return false;
}
/// <summary>
/// <see cref="PropertyDescriptor.ResetValue"/>
/// </summary>
public override void ResetValue(object component)
{
}
/// <summary>
/// <see cref="PropertyDescriptor.ShouldSerializeValue"/>
/// </summary>
public override bool ShouldSerializeValue(object component)
{
return false;
}
#endregion
}