C# WinForm DataGridView让DataPropertyName支持复杂属性

2021-05-04 09:26

阅读:443

标签:return   wce   gevent   .com   height   bsp   效果   rabl   代码   

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

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; }
    }
View Code

 

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

技术分享

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

 1  private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
 2         {
 3             if (e.Value != null) return;
 4             try
 5             {
 6                 var dgv = (DataGridView)sender;
 7                 object obj = null;
 8                 var source = dgv.DataSource as BindingSource;
 9                 if (source != null)
10                 {
11                     obj = ((IEnumerableobject>)source.DataSource).ElementAt(e.RowIndex);
12                 }
13                 else if (dgv.DataSource is IEnumerableobject>)
14                 {
15                     obj = ((IEnumerableobject>)dgv.DataSource).ElementAt(e.RowIndex);
16                 }
17                 var col = dgv.Columns[e.ColumnIndex];
18                 var props = col.DataPropertyName.Split(.);
19                 foreach (var prop in props)
20                 {
21                     if (obj == null) return;
22                     var p = obj.GetType().GetProperty(prop);
23                     if (p == null) return;
24                     obj = p.GetValue(obj, null);
25                 }
26                 e.Value = obj;
27             }
28             catch
29             {
30                 //ignore
31             }
32         }

 

 

效果:

bindingSource填充数据

技术分享技术分享
 bindingForForm2BindingSource.DataSource = new List
            {
                new BindingForForm2 {Age = 10, Height = 120, Name = "xxx", ClassTest = new ClassTest {S1 = "sdjfkljlk"}},
                new BindingForForm2 {Age = 12, Height = 120, Name = "asd", ClassTest = new ClassTest {S2 = "ccccccc"}},
                new BindingForForm2 {Age = 14, Height = 120, Name = "asdd"}
            };
View Code

GridView显示

技术分享

 

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

C# WinForm DataGridView让DataPropertyName支持复杂属性

标签:return   wce   gevent   .com   height   bsp   效果   rabl   代码   

原文地址:http://www.cnblogs.com/JmlSaul/p/7726233.html


评论


亲,登录后才可以留言!