datagridview 绑定复杂对象

kingastar 2008-01-03 03:32:04

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 应该写什么?
这种对象里面的属性仍是一个复杂对象怎么处理?
...全文
652 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
xcf007 2012-09-26
  • 打赏
  • 举报
回复
kingastar给的帖子里是正确答案,比较简单。
kingastar 2008-02-13
  • 打赏
  • 举报
回复
http://blogs.msdn.com/msdnts/archive/2007/01/19/how-to-bind-a-datagridview-column-to-a-second-level-property-of-a-data-source.aspx

可以参考一下
changjiangzhibin 2008-01-07
  • 打赏
  • 举报
回复
study
zmaini1420 2008-01-07
  • 打赏
  • 举报
回复
UP!~
jian_xiaowei 2008-01-06
  • 打赏
  • 举报
回复
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
}
jian_xiaowei 2008-01-06
  • 打赏
  • 举报
回复
看看TypedList.
vwxyzh 2008-01-04
  • 打赏
  • 举报
回复
方案1:建立一个视图类,绑定到视图类的集合上
方案2:确定影射关系(可以用Xml或其他),创建一个实现ITypedList的类,解析影射关系
方案3:确定影射关系(可以用Xml或其他),用Emit根据影射关系动态创建一个类型,绑定到动态类型的集合
kingastar 2008-01-03
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20070725/16/13aeb1a5-90e1-4b31-9690-cbe045630618.html
看来有人问了类似的问题,但是也没有答案。这里记录一下
请教一个C# Winform的数据邦定问题(lxsfg)
kingastar 2008-01-03
  • 打赏
  • 举报
回复
自己UP一下,没有办法么?
zmaini1420 2008-01-03
  • 打赏
  • 举报
回复
UP!~~~
chengqscjh 2008-01-03
  • 打赏
  • 举报
回复
Address类里面还有很多信息,比如,地址,邮编。。。。 这样的话你把信息都归为到了Address类,也可以,但是对于这种用法虽然抽象了,但是用起来比较麻烦,相当于重载.<%# DataBinder.Eval(Container.DataItem, "变量") %>
kingastar 2008-01-03
  • 打赏
  • 举报
回复

1、如果不改变类的设计,是没有办法绑定?

2、User类设计的不正确么?Address类里面还有很多信息,比如,地址,邮编。。。。

那么要显示出来一定要如下定义么?
public string Phone
{
get{return adress.Phone;}
set{adress.Phone= value;}
}

至少从类的设计来说,我觉得是多余的。
xuwenzhuo 2008-01-03
  • 打赏
  • 举报
回复
User这个类设计不正确,重新整理一下。
changjiangzhibin 2008-01-03
  • 打赏
  • 举报
回复
绑定数据源,绑定列值
xuwenzhuo 2008-01-03
  • 打赏
  • 举报
回复
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;}
}
}
不就可以了吗??????

kingastar 2008-01-03
  • 打赏
  • 举报
回复
首先,我的datagridview是在winform里面。
其次,
name,age绑定的dataPropertyName很好写。
主要是
address不是一个简单类型,而是一个复杂对象。
需要把adrress的phone显示出来。
CMIC 2008-01-03
  • 打赏
  • 举报
回复
dataPropertyName 直接写属性名Name,phone 什么的
页面这样写就行
<%# DataBinder.Eval(Container.DataItem, "name") %>

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧