protected
void
Page_Load(
object
sender, EventArgs e)
{
List<Employee> employees =
new
List<Employee>();
List<EmployeeDepartments> departments =
new
List<EmployeeDepartments>();
departments.Add(
new
EmployeeDepartments { Mark = 48, Department =
"Physics"
});
departments.Add(
new
EmployeeDepartments { Mark = 45, Department =
"Chemistry"
});
employees.Add(
new
Employee { Id = 1, Name =
"John Hammond"
, EmployeeDetails = departments });
departments =
new
List<EmployeeDepartments>();
departments.Add(
new
EmployeeDepartments { Mark = 50, Department =
"Mathmatics"
});
departments.Add(
new
EmployeeDepartments { Mark = 49, Department =
"Biology"
});
employees.Add(
new
Employee { Id = 2, Name =
"Mudassar Khan"
, EmployeeDetails = departments });
gvEmployees.DataSource = CreateNestedDataTable<Employee, EmployeeDepartments>(employees,
"EmployeeDetails"
);
gvEmployees.DataBind();
}
public
class
Employee
{
public
int
Id {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
List<EmployeeDepartments> EmployeeDetails {
get
;
set
; }
}
public
class
EmployeeDepartments
{
public
string
Department {
get
;
set
; }
public
int
Mark {
get
;
set
; }
}
public
DataTable CreateNestedDataTable<TOuter, TInner>(IEnumerable<TOuter> list,
string
innerListPropertyName)
{
PropertyInfo[] outerProperties =
typeof
(TOuter).GetProperties().Where(pi => pi.Name != innerListPropertyName).ToArray();
PropertyInfo[] innerProperties =
typeof
(TInner).GetProperties();
MethodInfo innerListGetter =
typeof
(TOuter).GetProperty(innerListPropertyName).GetGetMethod(
true
);
DataTable table =
new
DataTable();
foreach
(PropertyInfo pi
in
outerProperties)
{
table.Columns.Add(pi.Name, Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType);
}
foreach
(PropertyInfo pi
in
innerProperties)
{
table.Columns.Add(pi.Name, Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType);
}
foreach
(TOuter outerItem
in
list)
{
var innerList = innerListGetter.Invoke(outerItem,
null
)
as
IEnumerable<TInner>;
if
(innerList ==
null
|| innerList.Count() == 0)
{
DataRow row = table.NewRow();
foreach
(PropertyInfo pi
in
outerProperties)
{
row[pi.Name] = pi.GetValue(outerItem,
null
) ?? DBNull.Value;
}
table.Rows.Add(row);
}
else
{
foreach
(
object
innerItem
in
innerList)
{
DataRow row = table.NewRow();
foreach
(PropertyInfo pi
in
outerProperties)
{
row[pi.Name] = pi.GetValue(outerItem,
null
) ?? DBNull.Value;
}
foreach
(PropertyInfo pi
in
innerProperties)
{
row[pi.Name] = pi.GetValue(innerItem,
null
) ?? DBNull.Value;
}
table.Rows.Add(row);
}
}
}
return
table;
}