解决方法:将List转为DataTable,然后绑定到DataGrid,即可解决。
1 /// <summary> 2 /// 将集合类转换成DataTable 3 /// </summary> 4 /// <param name="list">集合</param> 5 public static DataTable ToDataTable(IList list) 6 { 7 DataTable result = new DataTable(); 8 if (list.Count > 0) 9 { 10 PropertyInfo[] propertys = list[0].GetType().GetProperties(); 11 foreach (PropertyInfo pi in propertys) 12 { 13 result.Columns.Add(pi.Name, pi.PropertyType); 14 } 15 16 17 for (int i = 0; i < list.Count; i++) 18 { 19 ArrayList tempList = new ArrayList(); 20 foreach (PropertyInfo pi in propertys) 21 { 22 object obj = pi.GetValue(list[i], null); 23 tempList.Add(obj); 24 } 25 object[] array = tempList.ToArray(); 26 result.LoadDataRow(array, true); 27 } 28 } 29 return result; 30 }
时间: 2024-10-14 09:58:23