首先給Grid添加BindingSource,類型為BindingForForm2。或者設置Grid的DataSource為IEnumerable<BindingForForm2>。 BindingForForm2類型如下。 public class BindingForForm2 { public ...
首先給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