好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁(yè) > 知識(shí)庫(kù) > DataGridView展開(kāi)與收縮功能實(shí)現(xiàn)

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

熱門(mén)標(biāo)簽:聊城智能電銷(xiāo)機(jī)器人電話 安陸市地圖標(biāo)注app 海東防封電銷(xiāo)卡 云南外呼系統(tǒng)代理 上海市三維地圖標(biāo)注 寧德防封版電銷(xiāo)卡 南昌自動(dòng)外呼系統(tǒng)線路 西寧電銷(xiāo)外呼系統(tǒng)公司 辦公用地圖標(biāo)注網(wǎng)點(diǎn)怎么操作

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

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

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

首先,創(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('門(mén)診外室',1,'1111','XXX醫(yī)院') 
insert into Department values('門(mén)診內(nèi)科',1,'2222','XXX醫(yī)院') 
insert into Department values('門(mén)診手術(shù)',1,'3333','XXX醫(yī)院') 
insert into Department values('門(mén)診兒科',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ǎ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。

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)展開(kāi) 
          row.Cells["IsEx"].Value = "false"; 
          //用于顯示展開(kāi)或收縮符號(hào),為了簡(jiǎn)單我就直接用字符串了,其實(shí)用圖片比較美觀 
          row.Cells["EX"].Value = "+"; 
        } 
      } 
    } 

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

插入子節(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)展開(kāi) 
        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 = "+"; 
      } 

這里面通過(guò)比較ID來(lái)唯一確定一行,循環(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); 
          } 

上面的做法是通過(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)該再加一列:

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

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

展開(kāi)行

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)展開(kāi) 
        this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true"; 
        this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-"; 
      } 

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

收縮行

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)展開(kāi)收縮的吧,希望大家不僅知道是如何實(shí)現(xiàn)的還要?jiǎng)邮謱?shí)驗(yàn)一番,才不枉小編辛苦整理此文章哦

您可能感興趣的文章:
  • C# DataGridView綁定數(shù)據(jù)源的方法
  • C#窗體控件DataGridView常用設(shè)置
  • Winform在DataGridView中顯示圖片
  • WinForm中DataGridView折疊控件【超好看】
  • C#自定義DataGridViewColumn顯示TreeView
  • C#中DataGridView動(dòng)態(tài)添加行及添加列的方法
  • DataGridView使用自定義控件實(shí)現(xiàn)簡(jiǎn)單分頁(yè)功能(推薦)

標(biāo)簽:衢州 崇左 青海 洛陽(yáng) 汕尾 贛州 南寧

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《DataGridView展開(kāi)與收縮功能實(shí)現(xiàn)》,本文關(guān)鍵詞  DataGridView,展開(kāi),與,收縮,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《DataGridView展開(kāi)與收縮功能實(shí)現(xiàn)》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于DataGridView展開(kāi)與收縮功能實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 好吊色几万部永久免费视频| 放荡勾人绿茶女(h)| 色多多www视频在线观看免费| 啊别?了快cao我啊~坤坤| 他掀开裙子把舌头伸进去添视频| 撕开胸罩一边亲一边摸| 好大灬好硬灬好爽灬无码| 视色4se影院在线观看| 电车里的日日液液(疯狂电车)最新章节| 亚洲福利在线看| 我看一级片| 成人无码A片毛片涂抹按油| 啊灬啊灬啊灬深灬快用力的视频 | 黄蓉娇羞迎合黄药师| 一般人戴49mm是不是小| 老师你下面太紧进不去小说| 欧美黄色特级片| 女性下面毛毛沟湿的原因| 日本男男gayjapanesepron| 一个人高清观看日本完整版视频软件| www精品一区二区三区四区| 全球降临:不朽国度| 精品人妻一区二区三区午夜剧场| 免费激情网| 对魔忍浅葱~欲の奴隷娼动漫| 护士解开衣服给老头喂奶| 77777五月色婷婷丁香视频| 黄色三级理论片| 久久悠悠| 日本人妻av一区二区三区电影| 很很日很很干| 亚洲狼综合人网站| 顶点韩漫| 91亚洲狠狠色综合久久久久久久| 久久精品国产99久久6动漫亮点| 国产一区二区三区播放| 韩国电影小嫂子| 极品羞羞久久久久久久精品| 一路向西手机在线| 黄色一级欧美| 国产做受???高潮漫动|