C# WinForm DataGridView让DataPropertyName支持复杂属性
2021-05-04 09:26
标签:return wce gevent .com height bsp 效果 rabl 代码 首先给Grid添加BindingSource,类型为BindingForForm2。或者设置Grid的DataSource为IEnumerable BindingForForm2类型如下。 我们想在Grid上直接显示BindingForForm2中ClassTest属性的S1和S2属性。可以如下图设置DataPropertyName。直接设置用属性点的方式。 然后如下注册DataGridView的CellFormatting事件即可。代码大致意思是,先取到当前选中行的Object(此处为BindingForForm2),然后取到DataPropertyName的设置,再循环用反射读取想要的值。 效果: bindingSource填充数据 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 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; }
}
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 }
bindingForForm2BindingSource.DataSource = new List
文章标题:C# WinForm DataGridView让DataPropertyName支持复杂属性
文章链接:http://soscw.com/index.php/essay/82201.html