欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

我們來說一下在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('門診外室',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)的時(shí)候,在父節(jié)點(diǎn)下插入新的DataGridViewRow;收縮父節(jié)點(diǎn)的時(shí)候,在父節(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"; 
          //用于顯示展開或收縮符號(hào),為了簡單我就直接用字符串了,其實(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中新增兩個(gè)列:

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

RowCount:用來保存該行下插入的子節(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ù)

展開行

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 = "-"; 
      } 

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

相關(guān)文章

  • asp.net獲取ListView與gridview中當(dāng)前行的行號(hào)

    asp.net獲取ListView與gridview中當(dāng)前行的行號(hào)

    這篇文章主要介紹了asp.net獲取ListView與gridview中當(dāng)前行的行號(hào),實(shí)例分析了asp.net針對ListView與gridview獲取行號(hào)的實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-01-01
  • .NET Core中RabbitMQ使用死信隊(duì)列的實(shí)現(xiàn)

    .NET Core中RabbitMQ使用死信隊(duì)列的實(shí)現(xiàn)

    本文主要介紹了.NET Core中RabbitMQ使用死信隊(duì)列的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • asp.net無法加載oci.dll等錯(cuò)誤的解決方法

    asp.net無法加載oci.dll等錯(cuò)誤的解決方法

    .net在windows2003下訪問oracle9i提示“無法加載oci.dll”或"無法在dll oci.dll中找到名為ocienvcreate的入口點(diǎn) "的修復(fù)方法
    2013-10-10
  • asp.net EXECUTENONQUERY()返回值介紹

    asp.net EXECUTENONQUERY()返回值介紹

    前些日子作一些數(shù)據(jù)項(xiàng)目的時(shí)候 在ADO.NET 中處理 ExecuteNonQuery()方法時(shí),總是通過判斷其返回值是否大于0來判斷操作時(shí)候成功 。但是實(shí)際上并不是這樣的,下面詳細(xì)介紹一下,有需要的朋友可以參考
    2013-08-08
  • MVC HtmlHelper擴(kuò)展類(PagingHelper)實(shí)現(xiàn)分頁功能

    MVC HtmlHelper擴(kuò)展類(PagingHelper)實(shí)現(xiàn)分頁功能

    這篇文章主要為大家詳細(xì)介紹了MVC HtmlHelper擴(kuò)展,實(shí)現(xiàn)分頁功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • .Net中MoongoDB的簡單調(diào)用圖文教程

    .Net中MoongoDB的簡單調(diào)用圖文教程

    這篇文章主要給大家介紹了關(guān)于.Net中MoongoDB的簡單調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • .NET中間件與VUE攔截器聯(lián)合使用詳情

    .NET中間件與VUE攔截器聯(lián)合使用詳情

    這篇文章主要介紹了.NET中間件與VUE攔截器聯(lián)合使用詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Asp.net 網(wǎng)站性能優(yōu)化二則分享

    Asp.net 網(wǎng)站性能優(yōu)化二則分享

    Web服務(wù)器的性能優(yōu)化有很多資料介紹了,多臺(tái)主機(jī)負(fù)載均衡,查詢結(jié)果的多級(jí)緩存,數(shù)據(jù)庫索引優(yōu)化等都是常見的優(yōu)化手段。
    2011-08-08
  • c# 讀取文件內(nèi)容存放到int數(shù)組 array.txt

    c# 讀取文件內(nèi)容存放到int數(shù)組 array.txt

    c# 讀取文本的內(nèi)容,并且將內(nèi)容保存到int數(shù)組中,大家可以學(xué)習(xí)到c#一些數(shù)組跟讀取內(nèi)容的函數(shù)。
    2009-04-04
  • ASP.NET 網(wǎng)站開發(fā)中常用到的廣告效果代碼

    ASP.NET 網(wǎng)站開發(fā)中常用到的廣告效果代碼

    在ASP.NET項(xiàng)目開發(fā)中,會(huì)被要求添加廣告,有翻屏效果、有廣告輪流顯示、飄浮廣告、左側(cè)廣告、右側(cè)廣告等。
    2010-04-04

最新評(píng)論