日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
玩轉(zhuǎn)DataGridView之行的展開(kāi)與收縮

很多數(shù)據(jù)都有父節(jié)點(diǎn)與子節(jié)點(diǎn),我們希望單擊父節(jié)點(diǎn)的時(shí)候可以展開(kāi)父節(jié)點(diǎn)下的子節(jié)點(diǎn)數(shù)據(jù)。

創(chuàng)新互聯(lián)于2013年開(kāi)始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元吉安做網(wǎng)站,已為上家服務(wù),為吉安各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

比如一個(gè)醫(yī)院科室表,有父科室與子科室,點(diǎn)擊父科室后,在父科室下面可以展現(xiàn)該科室下的所有子科室。

我們來(lái)說(shuō)一下在DataGridView中如何實(shí)現(xiàn)這個(gè)功能。

首先,創(chuàng)建示例數(shù)據(jù):

示例數(shù)據(jù)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醫(yī)院')  
  11. insert into Department values('門診內(nèi)科',1,'2222','XXX醫(yī)院')  
  12. insert into Department values('門診手術(shù)',1,'3333','XXX醫(yī)院')  
  13. insert into Department values('門診兒科',1,'4444','XXX醫(yī)院')  
  14. insert into Department values('神經(jīng)內(nèi)室',2,'5555','XXX醫(yī)院')  
  15. insert into Department values('神經(jīng)外科',2,'6666','XXX醫(yī)院')  
  16. insert into Department values('住院手術(shù)',2,'7777','XXX醫(yī)院')  
  17. insert into Department values('住院康復(fù)',2,'8888','XXX醫(yī)院') 

其實(shí)思路很簡(jiǎn)單,就是在展開(kāi)父節(jié)點(diǎn)的時(shí)候,在父節(jié)點(diǎn)下插入新的DataGridViewRow;收縮父節(jié)點(diǎn)的時(shí)候,在父節(jié)點(diǎn)下刪除該子節(jié)點(diǎn)的DataGridViewRow。

為了簡(jiǎn)便,代碼中的數(shù)據(jù)讀取我都直接硬編碼了。

加載父節(jié)點(diǎn)數(shù)據(jù),除了數(shù)據(jù)庫(kù)中的列外我還新加了兩列: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.                     //用于顯示該行是否已經(jīng)展開(kāi)  
  15.                     row.Cells["IsEx"].Value = "false";  
  16.                     //用于顯示展開(kāi)或收縮符號(hào),為了簡(jiǎn)單我就直接用字符串了,其實(shí)用圖片比較美觀  
  17.                     row.Cells["EX"].Value = "+";  
  18.                 }  
  19.             }  
  20.         } 

下面就是Cell的單擊事件了,分別在事件中寫(xiě)展開(kāi)的插入與收縮的刪除.

插入子節(jié)點(diǎn):

 
 
 
  1.  
  2. string isEx=this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value.ToString();  
  3.             if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx=="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.                 }  
  21.                 //將IsEx設(shè)置為true,標(biāo)明該節(jié)點(diǎn)已經(jīng)展開(kāi)  
  22.                 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true";  
  23.                 this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-"; 

刪除子節(jié)點(diǎn):

 
 
 
  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設(shè)置為false,標(biāo)明該節(jié)點(diǎn)已經(jīng)收縮  
  20.                this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false";  
  21.                this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+";  
  22.            } 

這里面通過(guò)比較ID來(lái)***確定一行,循環(huán)比較多,因?yàn)樽庸?jié)點(diǎn)是緊接著父節(jié)點(diǎn)的,我們可以確定子節(jié)點(diǎn)所在的行數(shù),所以用RemoveAt()方法更好。

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

上面的做法是通過(guò)不斷的插入與刪除來(lái)實(shí)現(xiàn),但這樣與數(shù)據(jù)庫(kù)的交互變得很頻繁。更好的做法應(yīng)該是插入一次,然后通過(guò)隱藏或顯示行來(lái)實(shí)現(xiàn)我們的效果。

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

IsInsert:用來(lái)判斷該行是否已經(jīng)有插入子節(jié)點(diǎn)數(shù)據(jù)

RowCount:用來(lái)保存該行下插入的子節(jié)點(diǎn)數(shù)量。

在方法DataGridBing中,綁定數(shù)據(jù)時(shí),應(yīng)該再加一列:

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

而在增加節(jié)點(diǎn)的時(shí)候,我們要多做一個(gè)判斷,如果IsInsert為false就插入數(shù)據(jù),如果為true就顯示數(shù)據(jù)

展開(kāi)行

 
 
 
  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.                     //顯示數(shù)據(jù)  
  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設(shè)置為true,標(biāo)明該節(jié)點(diǎn)已經(jīng)展開(kāi)  
  34.                 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true";  
  35.                 this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-";  
  36.             } 

收縮的時(shí)候,我們直接隱藏行就可以了.

收縮行

 
 
 
  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設(shè)置為false,標(biāo)明該節(jié)點(diǎn)已經(jīng)收縮  
  10.                 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false";  
  11.                 this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+";  
  12.             } 

原文鏈接:http://www.cnblogs.com/Gyoung/archive/2012/05/04/2482392.html


當(dāng)前名稱:玩轉(zhuǎn)DataGridView之行的展開(kāi)與收縮
標(biāo)題來(lái)源:http://www.5511xx.com/article/dhccppe.html