玩转DataGridView之行的展开与收缩

发表于:2012-5-07 09:52

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Gyoung    来源:51Testing软件测试网采编

  而在增加节点的时候,我们要多做一个判断,如果IsInsert为false就插入数据,如果为true就显示数据

  展开行

  1. if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx=="false")  
  2.             {  
  3.                 if (this.dataGridView1.Rows[e.RowIndex].Cells["IsInsert"].Value.ToString() == "false")  
  4.                 {  
  5.                     string id = this.dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();  
  6.                     DataTable table = GetDataTable("select * from Department where DparentId=" + id);  
  7.                     if (table.Rows.Count > 0)  
  8.                     {  
  9.                         //插入行  
  10.                         this.dataGridView1.Rows.Insert(e.RowIndex + 1, table.Rows.Count);  
  11.                         for (int i = 0; i < table.Rows.Count; i++)  
  12.                         {  
  13.                             DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex + i + 1];  
  14.                             row.DefaultCellStyle.BackColor = Color.CadetBlue;  
  15.                             row.Cells["ID"].Value = table.Rows[i]["ID"];  
  16.                             row.Cells["DName"].Value = table.Rows[i]["DName"];  
  17.                             row.Cells["Daddress"].Value = table.Rows[i]["Daddress"];  
  18.                             row.Cells["Dtelphone"].Value = table.Rows[i]["Dtelphone"];  
  19.                         }  
  20.                         this.dataGridView1.Rows[e.RowIndex].Cells["IsInsert"].Value = "true";  
  21.                         this.dataGridView1.Rows[e.RowIndex].Cells["RowCount"].Value = table.Rows.Count;  
  22.                     }  
  23.                 }  
  24.                 else 
  25.                 {  
  26.                     //显示数据  
  27.                     int RowCount = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["RowCount"].Value);  
  28.                     for (int i = 1; i <= RowCount; i++)  
  29.                     {  
  30.                         this.dataGridView1.Rows[e.RowIndex + i].Visible = true;  
  31.                     }  
  32.                 }  
  33.                 //将IsEx设置为true,标明该节点已经展开  
  34.                 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true";  
  35.                 this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-";  
  36.             }

  收缩的时候,我们直接隐藏行就可以了。

  收缩行

  1. if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx == "true")  
  2.             {  
  3.                 int RowCount = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["RowCount"].Value);  
  4.                 for (int i = 1; i <= RowCount; i++)  
  5.                 {  
  6.                     //隐藏行  
  7.                     this.dataGridView1.Rows[e.RowIndex + i].Visible = false;  
  8.                 }  
  9.                 ////将IsEx设置为false,标明该节点已经收缩  
  10.                 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false";  
  11.                 this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+";  
  12.             }

33/3<123
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号