我有一个实体类,里面有个属性是另个实体类(用于装载表连接值),在datagridview里应该怎么显示?比如:
员工表:
public class Employee
{
public string employeeId;
.... public Department department;}部门表:
public class Department
{
public string departmentId;
public string departmentName;
....
}我想在datagridvidw绑定List<Employee>后,显示员工的部门名称,这里怎么写?....
dgvEmployee.Columns.Add("departmentName", "部门名称");
....dgvEmployee.Columns[1].DataPropertyName = ???
员工表:
public class Employee
{
public string employeeId;
.... public Department department;}部门表:
public class Department
{
public string departmentId;
public string departmentName;
....
}我想在datagridvidw绑定List<Employee>后,显示员工的部门名称,这里怎么写?....
dgvEmployee.Columns.Add("departmentName", "部门名称");
....dgvEmployee.Columns[1].DataPropertyName = ???
....dgvEmployee.Columns[1].DataPropertyName =departmentName
Employee 继承Department 或者直接再建一个类
填充
最后绑定这个类的集合就行了。
private void dgvEmployeeAffair_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
//获得当前行
DataGridViewRow row = (sender as DataGridView).Rows[e.RowIndex]; //行数据源
EmployeeAffairEntity empAff = row.DataBoundItem as EmployeeAffairEntity;
if (empAff == null)
{
return;
} /**----------根据行的列名重新绑定数据---------**/
if (empAff.Employee != null)
{
//绑定嵌套类的属性
row.Cells[1].Value = empAff.Employee.EmployeeName;
}
}结贴了,谢谢各位的回复。