首先给Grid添加BindingSource,类型为BindingForForm2。或者设置Grid的DataSource为IEnumerable<BindingForForm2>。

BindingForForm2类型如下。

 public class BindingForForm2
{
public int Age { get; set; }
public string Name { get; set; }
public int Height { get; set; }
public int Weight { get; set; }
public ClassTest ClassTest { get; set; } } public class ClassTest
{
public string S1 { get; set; }
public string S2 { get; set; }
}

我们想在Grid上直接显示BindingForForm2中ClassTest属性的S1和S2属性。可以如下图设置DataPropertyName。直接设置用属性点的方式。

然后如下注册DataGridView的CellFormatting事件即可。代码大致意思是,先取到当前选中行的Object(此处为BindingForForm2),然后取到DataPropertyName的设置,再循环用反射读取想要的值。

  private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.Value != null) return;
try
{
var dgv = (DataGridView)sender;
object obj = null;
var source = dgv.DataSource as BindingSource;
if (source != null)
{
obj = ((IEnumerable<object>)source.DataSource).ElementAt(e.RowIndex);
}
else if (dgv.DataSource is IEnumerable<object>)
{
obj = ((IEnumerable<object>)dgv.DataSource).ElementAt(e.RowIndex);
}
var col = dgv.Columns[e.ColumnIndex];
var props = col.DataPropertyName.Split('.');
foreach (var prop in props)
{
if (obj == null) return;
var p = obj.GetType().GetProperty(prop);
if (p == null) return;
obj = p.GetValue(obj, null);
}
e.Value = obj;
}
catch
{
//ignore
}
}

效果:

bindingSource填充数据

 bindingForForm2BindingSource.DataSource = new List<BindingForForm2>
{
new BindingForForm2 {Age = , Height = , Name = "xxx", ClassTest = new ClassTest {S1 = "sdjfkljlk"}},
new BindingForForm2 {Age = , Height = , Name = "asd", ClassTest = new ClassTest {S2 = "ccccccc"}},
new BindingForForm2 {Age = , Height = , Name = "asdd"}
};

GridView显示

转载请注明出处。 http://www.cnblogs.com/JmlSaul/p/7726233.html

C# WinForm DataGridView让DataPropertyName支持复杂属性的更多相关文章

  1. C#实现WinForm DataGridView控件支持叠加数据绑定

    我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...

  2. C# winform DataGridView 常见属性

    C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行.列的隐藏和删 ...

  3. Winform Datagridview 单元格html格式化支持富文本

    Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...

  4. winform datagridview 绑定泛型集合变得不支持排序的解决方案

    原文:winform datagridview 绑定泛型集合变得不支持排序的解决方案 案例: 环境:Winform程序 控件:Datagridview 现象:Datagridview控件绑定到List ...

  5. winform DataGridView的虚模式填充,CellValueNeeded事件的触发条件

    虚模式填充常用来处理大量数据,某个字段的显示问题. DataGridView是.net 2.0新增的表格数据编辑和显示控件,简单的数据显示和编辑,只需直接和数据源绑定就可以了. 对于 一些特殊情况,我 ...

  6. WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  7. [转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  8. 让IE6/IE7/IE8浏览器支持CSS3属性

    让IE6/IE7/IE8浏览器支持CSS3属性 一.下载 您可以狠狠地点击这里:ie-css3.htc,这个玩意儿是让IE浏览器支持CSS3表现的关键东东. 二.上面的是什么东西 首先说说.htc文件 ...

  9. 解决ie6下不支持fix属性,模拟固定定位

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. 201521123025《java程序设计》第11周学习总结

    1. 本周学习总结 2. 书面作业 Q1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synchronized修饰方法实现互斥同步访问,还有什么办法实现互斥同步 ...

  2. ul中li居中显示的table方法

    废话不多,贴代码 <ul> <li>1</li> <li>2</li> <li>3</li> <li>4 ...

  3. apache: apache-tomcat-6.0.35完整下载

    Index of /dist/tomcat/tomcat-6/v6.0.35/bin Name Last modified Size Description Parent Directory - ex ...

  4. python实例编写(5)--异常处理,截图,用例设计

    一.python的异常处理 异常抛出处理机制: 1.若在运行时发生异常,解释器会查找相应的处理语句(handler) 2.若在当前函数无法找到,就将异常传给上层的调用函数,看是否能处理 3.如果在最外 ...

  5. 1 Spring Cloud Eureka服务治理

    注:此随笔为读书笔记.<Spring Cloud微服务实战> 什么是微服务? 微服务是将一个原本独立的系统拆分成若干个小型服务(一般按照功能模块拆分),这些小型服务都在各自独立的进程中运行 ...

  6. GCD之异步同步体会

    前面的博文也有写到同步异步,可能是看他人的博文,自己没有实验,感觉理解不深,所以就敲了些代码比较一下串行.并行分别对应的同步.异步. 1.首先创建串行.并行线程队列 1 2 dispatch_queu ...

  7. Nexus 私有仓库搭建与 Maven 集成

    Nexus 私有仓库搭建与 Maven 集成 |作者:RexFang |出处:http://www.cnblogs.com/rexfang/ |关于作者:Java 程序员一枚 |版权:本文版权归作者和 ...

  8. Cornfields poj2019 二维RMQ

    Cornfields Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit S ...

  9. Digital Square 搜索

    Digital Square Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  10. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)

    http://codeforces.com/contest/831 A. Unimodal Array time limit per test 1 second memory limit per te ...