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

本文介绍如何在DataGridView中显示复杂对象的属性,通过设置DataPropertyName并使用反射读取对象的嵌套属性值,实现数据的正确展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先给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; }
    }
复制代码
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 = ((IEnumerable<object>)source.DataSource).ElementAt(e.RowIndex);
12                 }
13                 else if (dgv.DataSource is IEnumerable<object>)
14                 {
15                     obj = ((IEnumerable<object>)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<BindingForForm2>
            {
                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

转载于:https://www.cnblogs.com/zwj1276952588/p/10538744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值