Th following tip/trick will help you create/load the datasource of a DataGridView
with very little effort to create Rows/Columns using Reflection. It is a reusuable piece of code across multiple Forms and/or DataGridView
controls. But think of requiring custom names for the columns before using this tip, since this creates column header(s) with just property name(s).
foreach (FieldInfo field in ((object)myObject).GetType().GetFields())
{
myTable.Columns.Add(new DataColumn(field.Name));
}
DataRow dr;
foreach (Class item in ObjectCollection)
{
dr = myTable.NewRow();
foreach (FieldInfo field in ((object)item).GetType().GetFields())
{
dr[field.Name] = field.GetValue(item);
}
myTable.Rows.Add(dr);
}
gridView.DataSource = myTable;