WinForm从objectcollection中加载数据到DataGridView

来源:互联网 发布:go语言 人工智能 编辑:程序博客网 时间:2024/05/17 22:19
//Create and Add columns to the DataTable.foreach (FieldInfo field in ((object)myObject).GetType().GetFields()){       myTable.Columns.Add(new DataColumn(field.Name));} //Load DataTableDataRow dr; foreach (Class item in ObjectCollection){   //Create a new Row for each item in the collection.   dr = myTable.NewRow();    foreach (FieldInfo field in ((object)item).GetType().GetFields())   {       //Set Column value       dr[field.Name] = field.GetValue(item);   }    myTable.Rows.Add(dr);} //Set the Data Source of the Grid.gridView.DataSource = myTable;