麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

DataGridView展開與收縮功能實現

2024-07-10 13:29:24
字體:
來源:轉載
供稿:網友

我們今天將要講到DataGridView之行的展開與收縮,包括功能是如何實現的,感興趣的小伙伴們可以參考一下

很多數據都有父節點與子節點,我們希望單擊父節點的時候可以展開父節點下的子節點數據。

比如一個醫院科室表,有父科室與子科室,點擊父科室后,在父科室下面可以展現該科室下的所有子科室。

我們來說一下在DataGridView中如何實現這個功能。

首先,創建示例數據:

示例數據SQL

 

 
  1. create table Department  
  2. (  
  3. ID int identity(1,1) not null,  
  4. DName varchar(20) null,  
  5. DparentId int null,  
  6. Dtelphone varchar(20) null,  
  7. Dhospital varchar(50) null 
  8. )  
  9.  
  10. insert into Department values('門診外室',1,'1111','XXX醫院')  
  11. insert into Department values('門診內科',1,'2222','XXX醫院')  
  12. insert into Department values('門診手術',1,'3333','XXX醫院')  
  13. insert into Department values('門診兒科',1,'4444','XXX醫院')  
  14. insert into Department values('神經內室',2,'5555','XXX醫院')  
  15. insert into Department values('神經外科',2,'6666','XXX醫院')  
  16. insert into Department values('住院手術',2,'7777','XXX醫院')  
  17. insert into Department values('住院康復',2,'8888','XXX醫院')  

其實思路很簡單,就是在展開父節點的時候,在父節點下插入新的DataGridViewRow;收縮父節點的時候,在父節點下刪除該子節點的DataGridViewRow。

為了簡便,代碼中的數據讀取我都直接硬編碼了。

加載父節點數據,除了數據庫中的列外我還新加了兩列:IsEx與EX。

 

 
  1. private void DataGridBing(DataTable table)  
  2. {  
  3. if (table.Rows.Count > 0)  
  4. {  
  5. for (int i = 0; i < table.Rows.Count; i++)  
  6. {  
  7.  
  8. int k = this.dataGridView1.Rows.Add();  
  9. DataGridViewRow row = this.dataGridView1.Rows[k];  
  10. row.Cells["ID"].Value = table.Rows[i]["ID"];  
  11. row.Cells["DName"].Value = table.Rows[i]["DName"];  
  12. row.Cells["Daddress"].Value = table.Rows[i]["Daddress"];  
  13. row.Cells["Dtelphone"].Value = table.Rows[i]["Dtelphone"];  
  14. //用于顯示該行是否已經展開  
  15. row.Cells["IsEx"].Value = "false";  
  16. //用于顯示展開或收縮符號,為了簡單我就直接用字符串了,其實用圖片比較美觀  
  17. row.Cells["EX"].Value = "+";  
  18. }  
  19. }  
  20. }  

下面就是Cell的單擊事件了,分別在事件中寫展開的插入與收縮的刪除.

插入子節點:

 

  1. string isEx=this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value.ToString();  
  2. if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx=="false")  
  3. {  
  4. string id = this.dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();  
  5. DataTable table = GetDataTable("select * from Department where DparentId="+id);  
  6. if (table.Rows.Count > 0)  
  7. {  
  8. //插入行  
  9. this.dataGridView1.Rows.Insert(e.RowIndex+1, table.Rows.Count);  
  10. for (int i = 0; i < table.Rows.Count; i++)  
  11. {  
  12. DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex + i+1];  
  13. row.DefaultCellStyle.BackColor = Color.CadetBlue;  
  14. row.Cells["ID"].Value = table.Rows[i]["ID"];  
  15. row.Cells["DName"].Value = table.Rows[i]["DName"];  
  16. row.Cells["Daddress"].Value = table.Rows[i]["Daddress"];  
  17. row.Cells["Dtelphone"].Value = table.Rows[i]["Dtelphone"];  
  18. }  
  19. }  
  20. //將IsEx設置為true,標明該節點已經展開  
  21. this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true";  
  22. this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-";  

刪除子節點:

 

 
  1. if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx == "true")  
  2. {  
  3. string id = this.dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();  
  4. DataTable table = GetDataTable("select * from Department where DparentId=" + id);  
  5. if (table.Rows.Count > 0)  
  6. {  
  7. //利用Remove  
  8. for (int i = 0; i < table.Rows.Count; i++)  
  9. {  
  10. foreach (DataGridViewRow row in this.dataGridView1.Rows)  
  11. {  
  12. if (row.Cells["ID"].Value.Equals(table.Rows[i]["ID"]))  
  13. {  
  14. this.dataGridView1.Rows.Remove(row);  
  15. }  
  16. }  
  17. }  
  18. }  
  19. ////將IsEx設置為false,標明該節點已經收縮  
  20. this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false";  
  21. this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+";  
  22. }  

這里面通過比較ID來唯一確定一行,循環比較多,因為子節點是緊接著父節點的,我們可以確定子節點所在的行數,所以用RemoveAt()方法更好。

 

 
  1. //利用RemoveAt  
  2. for (int i = table.Rows.Count; i > 0; i--)  
  3. {  
  4. //刪除行  
  5. this.dataGridView1.Rows.RemoveAt(i + e.RowIndex);  
  6. }  

上面的做法是通過不斷的插入與刪除來實現,但這樣與數據庫的交互變得很頻繁。更好的做法應該是插入一次,然后通過隱藏或顯示行來實現我們的效果。

為此,我們還要在grid中新增兩個列:

IsInsert:用來判斷該行是否已經有插入子節點數據

RowCount:用來保存該行下插入的子節點數量。

在方法DataGridBing中,綁定數據時,應該再加一列:

 

 
  1. //是否插入  
  2. row.Cells["IsInsert"].Value = "false";  

而在增加節點的時候,我們要多做一個判斷,如果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. }  

大家知道DataGridView是如何實現展開收縮的吧,希望大家不僅知道是如何實現的還要動手實驗一番,才不枉小編辛苦整理此文章哦

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久99精品久久久久久秒播蜜臀 | 午夜视频亚洲 | 黄网站在线免费 | 全视频tv | 九九精品视频观看 | 欧美日韩亚洲不卡 | 在线中文字幕亚洲 | 黄污网站在线观看 | 国产精品啪 | 久草在线公开视频 | 蜜桃网在线 | 久久精品日韩一区 | 成人永久免费视频 | 日本中文高清 | 午夜精品久久久久久中宇 | 国产91九色视频 | 久久一区三区 | 国产精品探花在线观看 | 免费一级毛片在线播放视频老 | 精品久久久久久久久久 | 99国产精品自拍 | 成人免费一区二区三区 | 宅男噜噜噜66国产免费观看 | 国产精品视频自拍 | 欧美成人免费电影 | 国产乱轮视频 | 久久tv免费国产高清 | 成人在线免费观看网址 | 九九热在线精品视频 | 久久久久久久久成人 | 久久久久久久久久久久久久av | 国产91丝袜在线播放 | av在线试看 | 欧美中文字幕一区二区 | 日本在线播放一区二区三区 | 久久久www免费看片 亚洲综合视频一区 | 97黄色网 | 国产精品久久久久久久亚洲按摩 | 成人在线不卡 | 国产精品一区在线看 | 天天色狠狠干 |