首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET Framework >

datagridview 绑定复杂对象,该如何处理

2012-10-09 
datagridview 绑定复杂对象C# codepublic class User{public User(){}public User(int vAge,string vName)

datagridview 绑定复杂对象

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


{
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
}

热点排行