<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        DataGridView展開與收縮功能實(shí)現(xiàn)

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:38:08
        文檔

        DataGridView展開與收縮功能實(shí)現(xiàn)

        DataGridView展開與收縮功能實(shí)現(xiàn):很多數(shù)據(jù)都有父節(jié)點(diǎn)與子節(jié)點(diǎn),我們希望單擊父節(jié)點(diǎn)的時候可以展開父節(jié)點(diǎn)下的子節(jié)點(diǎn)數(shù)據(jù)。 比如一個醫(yī)院科室表,有父科室與子科室,點(diǎn)擊父科室后,在父科室下面可以展現(xiàn)該科室下的所有子科室。 我們來說一下在DataGridView中如何實(shí)現(xiàn)這個功能。 首先,創(chuàng)建示例
        推薦度:
        導(dǎo)讀DataGridView展開與收縮功能實(shí)現(xiàn):很多數(shù)據(jù)都有父節(jié)點(diǎn)與子節(jié)點(diǎn),我們希望單擊父節(jié)點(diǎn)的時候可以展開父節(jié)點(diǎn)下的子節(jié)點(diǎn)數(shù)據(jù)。 比如一個醫(yī)院科室表,有父科室與子科室,點(diǎn)擊父科室后,在父科室下面可以展現(xiàn)該科室下的所有子科室。 我們來說一下在DataGridView中如何實(shí)現(xiàn)這個功能。 首先,創(chuàng)建示例

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

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

        我們來說一下在DataGridView中如何實(shí)現(xiàn)這個功能。

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

        示例數(shù)據(jù)SQL

        create table Department 
        ( 
         ID int identity(1,1) not null, 
         DName varchar(20) null, 
         DparentId int null, 
         Dtelphone varchar(20) null, 
         Dhospital varchar(50) null 
        ) 
         
        insert into Department values('門診外室',1,'1111','XXX醫(yī)院') 
        insert into Department values('門診內(nèi)科',1,'2222','XXX醫(yī)院') 
        insert into Department values('門診手術(shù)',1,'3333','XXX醫(yī)院') 
        insert into Department values('門診兒科',1,'4444','XXX醫(yī)院') 
        insert into Department values('神經(jīng)內(nèi)室',2,'5555','XXX醫(yī)院') 
        insert into Department values('神經(jīng)外科',2,'6666','XXX醫(yī)院') 
        insert into Department values('住院手術(shù)',2,'7777','XXX醫(yī)院') 
        insert into Department values('住院康復(fù)',2,'8888','XXX醫(yī)院') 
        

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

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

        加載父節(jié)點(diǎn)數(shù)據(jù),除了數(shù)據(jù)庫中的列外我還新加了兩列:IsEx與EX。

        private void DataGridBing(DataTable table) 
         { 
         if (table.Rows.Count > 0) 
         { 
         for (int i = 0; i < table.Rows.Count; i++) 
         { 
         
         int k = this.dataGridView1.Rows.Add(); 
         DataGridViewRow row = this.dataGridView1.Rows[k]; 
         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"]; 
         //用于顯示該行是否已經(jīng)展開 
         row.Cells["IsEx"].Value = "false"; 
         //用于顯示展開或收縮符號,為了簡單我就直接用字符串了,其實(shí)用圖片比較美觀 
         row.Cells["EX"].Value = "+"; 
         } 
         } 
         } 
        

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

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

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

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

        if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx == "true") 
         { 
         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) 
         { 
         //利用Remove 
         for (int i = 0; i < table.Rows.Count; i++) 
         { 
         foreach (DataGridViewRow row in this.dataGridView1.Rows) 
         { 
         if (row.Cells["ID"].Value.Equals(table.Rows[i]["ID"])) 
         { 
         this.dataGridView1.Rows.Remove(row); 
         } 
         } 
         } 
         } 
         ////將IsEx設(shè)置為false,標(biāo)明該節(jié)點(diǎn)已經(jīng)收縮 
         this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false"; 
         this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+"; 
         } 
        

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

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

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

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

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

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

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

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

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

        展開行

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

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

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        DataGridView展開與收縮功能實(shí)現(xiàn)

        DataGridView展開與收縮功能實(shí)現(xiàn):很多數(shù)據(jù)都有父節(jié)點(diǎn)與子節(jié)點(diǎn),我們希望單擊父節(jié)點(diǎn)的時候可以展開父節(jié)點(diǎn)下的子節(jié)點(diǎn)數(shù)據(jù)。 比如一個醫(yī)院科室表,有父科室與子科室,點(diǎn)擊父科室后,在父科室下面可以展現(xiàn)該科室下的所有子科室。 我們來說一下在DataGridView中如何實(shí)現(xiàn)這個功能。 首先,創(chuàng)建示例
        推薦度:
        標(biāo)簽: 功能 實(shí)現(xiàn) 展開
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲经典在线中文字幕| 亚洲一区无码精品色| 亚洲精品无码久久毛片波多野吉衣| 亚洲免费日韩无码系列| 久久精品国产精品亚洲人人| 国产免费区在线观看十分钟| 亚洲成A人片在线观看无码3D| 污网站在线观看免费| 亚洲精品视频在线看| 国产精品午夜免费观看网站 | 999在线视频精品免费播放观看| 亚洲最大福利视频网站| 91精品免费观看| 亚洲天堂一区二区三区四区| 欧美三级在线电影免费| 亚洲成av人片在线天堂无| 国产乱子影视频上线免费观看| 日韩在线视频免费| 国产AV无码专区亚洲Av| 最近中文字幕mv免费高清视频8| 亚洲国产高清在线精品一区| 无码一区二区三区免费视频 | 国产精品青草视频免费播放| 亚洲AV中文无码字幕色三| 久久成人国产精品免费软件| 亚洲最大天堂无码精品区| 免费在线观看一级毛片| 91免费福利视频| 亚洲日本乱码一区二区在线二产线| 成人免费一区二区三区在线观看| 免费在线观看一区| 亚洲午夜在线电影| 高清国语自产拍免费视频国产 | 亚洲午夜国产精品无码老牛影视| 1000部啪啪毛片免费看| 国产亚洲一卡2卡3卡4卡新区 | 国产三级电影免费观看| 免费在线中文日本| 亚洲午夜无码久久久久软件| 久久影院亚洲一区| 毛片基地免费视频a|