而在增加节点的时候,我们要多做一个判断,如果IsInsert为false就插入数据,如果为true就显示数据
展开行
- 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;
- }
- }
-
- 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;
- }
-
- this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false";
- this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+";
- }
|