create table Department
(
ID int identity(1,1) not null,
DName varchar(20) null,
DparentId int null,
Dtelphone varchar(20) null,
Dhospital varchar(50) null
)
insert into Department values('门诊外室',1,'1111','XXX医院')
insert into Department values('门诊内科',1,'2222','XXX医院')
insert into Department values('门诊手术',1,'3333','XXX医院')
insert into Department values('门诊儿科',1,'4444','XXX医院')
insert into Department values('神经内室',2,'5555','XXX医院')
insert into Department values('神经外科',2,'6666','XXX医院')
insert into Department values('住院手术',2,'7777','XXX医院')
insert into Department values('住院康复',2,'8888','XXX医院')
private void DataGridBing(DataTable table)
{
if (table.Rows.Count > 0)
{
for (int i = 0; i < table.Rows.Count; i++)
{
int k = this.dataGridView1.Rows.Add();
DataGridViewRow row = this.dataGridView1.Rows[k];
row.Cells["ID"].Value = table.Rows[i]["ID"];
row.Cells["DName"].Value = table.Rows[i]["DName"];
row.Cells["Daddress"].Value = table.Rows[i]["Daddress"];
row.Cells["Dtelphone"].Value = table.Rows[i]["Dtelphone"];
//用于显示该行是否已经展开
row.Cells["IsEx"].Value = "false";
//用于显示展开或收缩符号,为了简单我就直接用字符串了,其实用图片比较美观
row.Cells["EX"].Value = "+";
}
}
}
string isEx=this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value.ToString();
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx=="false")
{
string id = this.dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();
DataTable table = GetDataTable("select * from Department where DparentId="+id);
if (table.Rows.Count > 0)
{
//插入行
this.dataGridView1.Rows.Insert(e.RowIndex+1, table.Rows.Count);
for (int i = 0; i < table.Rows.Count; i++)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex + i+1];
row.DefaultCellStyle.BackColor = Color.CadetBlue;
row.Cells["ID"].Value = table.Rows[i]["ID"];
row.Cells["DName"].Value = table.Rows[i]["DName"];
row.Cells["Daddress"].Value = table.Rows[i]["Daddress"];
row.Cells["Dtelphone"].Value = table.Rows[i]["Dtelphone"];
}
}
//将IsEx设置为true,标明该节点已经展开
this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true";
this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-";
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx == "true")
{
string id = this.dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();
DataTable table = GetDataTable("select * from Department where DparentId=" + id);
if (table.Rows.Count > 0)
{
//利用Remove
for (int i = 0; i < table.Rows.Count; i++)
{
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (row.Cells["ID"].Value.Equals(table.Rows[i]["ID"]))
{
this.dataGridView1.Rows.Remove(row);
}
}
}
}
////将IsEx设置为false,标明该节点已经收缩
this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false";
this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+";
}
//利用RemoveAt
for (int i = table.Rows.Count; i > 0; i--)
{
//删除行
this.dataGridView1.Rows.RemoveAt(i + e.RowIndex);
}
//是否插入 row.Cells["IsInsert"].Value = "false";
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx=="false")
{
if (this.dataGridView1.Rows[e.RowIndex].Cells["IsInsert"].Value.ToString() == "false")
{
string id = this.dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();
DataTable table = GetDataTable("select * from Department where DparentId=" + id);
if (table.Rows.Count > 0)
{
//插入行
this.dataGridView1.Rows.Insert(e.RowIndex + 1, table.Rows.Count);
for (int i = 0; i < table.Rows.Count; i++)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex + i + 1];
row.DefaultCellStyle.BackColor = Color.CadetBlue;
row.Cells["ID"].Value = table.Rows[i]["ID"];
row.Cells["DName"].Value = table.Rows[i]["DName"];
row.Cells["Daddress"].Value = table.Rows[i]["Daddress"];
row.Cells["Dtelphone"].Value = table.Rows[i]["Dtelphone"];
}
this.dataGridView1.Rows[e.RowIndex].Cells["IsInsert"].Value = "true";
this.dataGridView1.Rows[e.RowIndex].Cells["RowCount"].Value = table.Rows.Count;
}
}
else
{
//显示数据
int RowCount = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["RowCount"].Value);
for (int i = 1; i <= RowCount; i++)
{
this.dataGridView1.Rows[e.RowIndex + i].Visible = true;
}
}
//将IsEx设置为true,标明该节点已经展开
this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true";
this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-";
}
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx == "true")
{
int RowCount = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["RowCount"].Value);
for (int i = 1; i <= RowCount; i++)
{
//隐藏行
this.dataGridView1.Rows[e.RowIndex + i].Visible = false;
}
////将IsEx设置为false,标明该节点已经收缩
this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false";
this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+";
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有