在另一个内部实现datagrid时,我的方案是
Datagridview1
将保留DepartmentID
列& DeptName
还有一个buttonColumn Expand
列,on_click
添加了一个文本列Employee
,并Employee
列EmployeeId
和Name
来自Datagridview2
代码是:
int count =0;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = dataGridView1.CurrentRow.Index;
if (dataGridView1.Columns.Contains("Expand") && e.ColumnIndex == dataGridView1.Columns["Expand"].Index)
{
if (count % 2 == 0)//for even count show the datagrid2 else hide
{
var d = dataGridView1.Rows[rowIndex].DataBoundItem as Test.Departments;
dataGridView2.DataSource = Test.EmployeeDataaccessLayer.GetAllEmployeesByDepartment(d.DeptId);
AddemployeeColumns();
/* foreach (DataGridViewRow r in dataGridView2.Rows)
{
int index = r.Index;
var dd = dataGridView2.Rows[index].DataBoundItem as Test.Employee;
dataGridView1["Employee", rowIndex].Value = dd;
}*/ This part to be correct
count++;
}
else
{
dataGridView2.Visible = false;
count++;
}
}
所以我基本上想要将DatagridView2
添加到显示员工详细信息的单元格Employee
。在我们绑定子网格的Asp.net
中更容易,但在windows窗体中如何实现它?