最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年09月27日 16:25:50   投稿:lijiao  
我們今天將要講到DataGridView之行的展開與收縮,包括功能是如何實現(xiàn)的,感興趣的小伙伴們可以參考一下

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

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

我們來說一下在DataGridView中如何實現(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ī)院') 

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

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

加載父節(jié)點數(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"; 
          //用于顯示展開或收縮符號,為了簡單我就直接用字符串了,其實用圖片比較美觀 
          row.Cells["EX"].Value = "+"; 
        } 
      } 
    } 

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

插入子節(jié)點:

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é)點已經(jīng)展開 
        this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true"; 
        this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-"; 

刪除子節(jié)點:

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é)點已經(jīng)收縮 
        this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false"; 
        this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+"; 
      } 

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

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

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

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

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

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

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

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

而在增加節(jié)點的時候,我們要多做一個判斷,如果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é)點已經(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é)點已經(jīng)收縮 
        this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false"; 
        this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+"; 
      } 

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

相關(guān)文章

  • 子窗口給父窗口賦值實現(xiàn)思路及案例演示

    子窗口給父窗口賦值實現(xiàn)思路及案例演示

    今天學(xué)習(xí)一下javascript實現(xiàn)從父窗口打開一個子窗口,在子窗口處理一些結(jié)果之后,把結(jié)果賦值于父窗口的文本框內(nèi),接下來介紹實現(xiàn)方法,感興趣的朋友可以了解下,希望本文對你有所幫助
    2013-01-01
  • .Net使用日志框架NLog

    .Net使用日志框架NLog

    這篇文章介紹了.Net使用日志框架NLog的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • SQL為查詢的結(jié)果加上序號(ROW_NUMBER) 合并多個查詢結(jié)果

    SQL為查詢的結(jié)果加上序號(ROW_NUMBER) 合并多個查詢結(jié)果

    SQL為查詢的結(jié)果加上序號(ROW_NUMBER) 合并多個查詢結(jié)果
    2010-03-03
  • asp.net Repeater 自遞增

    asp.net Repeater 自遞增

    asp.net Repeater 自遞增,方便需要遞增輸出數(shù)列的朋友
    2009-02-02
  • .NET Core日志配置的方法

    .NET Core日志配置的方法

    熟悉ASP.NET的開發(fā)者一定對web.config文件不陌生,這篇文章主要介紹了.NET Core日志配置的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • System.Web.Routing入門及進(jìn)階

    System.Web.Routing入門及進(jìn)階

    System.Web.Routing已經(jīng)作為一個程序集包含在.net3.5sp1中發(fā)布了。雖然我們并沒有在3.5sp1中發(fā)現(xiàn)Asp.net Mvc的蹤跡,但是亦以感覺到它離我們不遠(yuǎn)了
    2011-12-12
  • ASP.NET Core設(shè)置URLs的五種方法

    ASP.NET Core設(shè)置URLs的五種方法

    這篇文章主要介紹了ASP.NET Core設(shè)置URLs的五種方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • ASP.NET筆記之Session、http、web開發(fā)原則、xss漏洞的詳細(xì)介紹

    ASP.NET筆記之Session、http、web開發(fā)原則、xss漏洞的詳細(xì)介紹

    本篇文章小編為大家介紹,ASP.NET筆記之Session、http、web開發(fā)原則、xss漏洞詳細(xì)。需要的朋友參考下
    2013-04-04
  • .NET8中g(shù)RPC的使用方法詳解

    .NET8中g(shù)RPC的使用方法詳解

    gRPC是一種高性能、開源的遠(yuǎn)程過程調(diào)用(RPC)框架,基于 HTTP/2 協(xié)議,支持雙向流、頭部壓縮等特性,下面我們就來看看.NET8下gRPC的具體使用吧
    2025-03-03
  • asp.net core項目中如何使用html文件

    asp.net core項目中如何使用html文件

    這篇文章主要給大家介紹了關(guān)于asp.net core項目中如何使用html文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面是隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-02-02

最新評論

郸城县| 任丘市| 大化| 沙田区| 新晃| 东城区| 聂拉木县| 凌云县| 琼结县| 凭祥市| 永川市| 大余县| 喀喇| 新津县| 太仆寺旗| 保山市| 应城市| 五华县| 宁德市| 敦煌市| 博罗县| 夏河县| 南涧| 大渡口区| 农安县| 澄江县| 温泉县| 西平县| 镇远县| 利津县| 金堂县| 石棉县| 富平县| 敖汉旗| 抚州市| 沁阳市| 商洛市| 新巴尔虎右旗| 太白县| 漳平市| 上饶市|