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 = "+"; }
大家知道DataGridView是如何实现展开收缩的吧,希望大家不仅知道是如何实现的还要动手实验一番,才不枉小编辛苦整理此文章哦
您可能感兴趣的文章: