玩转DataGridView之行的展开与收缩

很多数据都有父节点与子节点,我们希望单击父节点的时候可以展开父节点下的子节点数据。

创新互联于2013年开始,是专业互联网技术服务公司,拥有项目成都做网站、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元吉安做网站,已为上家服务,为吉安各地企业和个人服务,联系电话:18980820575

比如一个医院科室表,有父科室与子科室,点击父科室后,在父科室下面可以展现该科室下的所有子科室。

我们来说一下在DataGridView中如何实现这个功能。

首先,创建示例数据:

示例数据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医院')  
  11. insert into Department values('门诊内科',1,'2222','XXX医院')  
  12. insert into Department values('门诊手术',1,'3333','XXX医院')  
  13. insert into Department values('门诊儿科',1,'4444','XXX医院')  
  14. insert into Department values('神经内室',2,'5555','XXX医院')  
  15. insert into Department values('神经外科',2,'6666','XXX医院')  
  16. insert into Department values('住院手术',2,'7777','XXX医院')  
  17. insert into Department values('住院康复',2,'8888','XXX医院') 

其实思路很简单,就是在展开父节点的时候,在父节点下插入新的DataGridViewRow;收缩父节点的时候,在父节点下删除该子节点的DataGridViewRow。

为了简便,代码中的数据读取我都直接硬编码了。

加载父节点数据,除了数据库中的列外我还新加了两列: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.                     //用于显示该行是否已经展开  
  15.                     row.Cells["IsEx"].Value = "false";  
  16.                     //用于显示展开或收缩符号,为了简单我就直接用字符串了,其实用图片比较美观  
  17.                     row.Cells["EX"].Value = "+";  
  18.                 }  
  19.             }  
  20.         } 

下面就是Cell的单击事件了,分别在事件中写展开的插入与收缩的删除.

插入子节点:

 
 
 
  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设置为true,标明该节点已经展开  
  22.                 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true";  
  23.                 this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-"; 

删除子节点:

 
 
 
  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设置为false,标明该节点已经收缩  
  20.                this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false";  
  21.                this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+";  
  22.            } 

这里面通过比较ID来***确定一行,循环比较多,因为子节点是紧接着父节点的,我们可以确定子节点所在的行数,所以用RemoveAt()方法更好。

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

上面的做法是通过不断的插入与删除来实现,但这样与数据库的交互变得很频繁。更好的做法应该是插入一次,然后通过隐藏或显示行来实现我们的效果。

为此,我们还要在grid中新增两个列:

IsInsert:用来判断该行是否已经有插入子节点数据

RowCount:用来保存该行下插入的子节点数量。

在方法DataGridBing中,绑定数据时,应该再加一列:

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

而在增加节点的时候,我们要多做一个判断,如果IsInsert为false就插入数据,如果为true就显示数据

展开行

 
 
 
  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.                     //显示数据  
  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设置为true,标明该节点已经展开  
  34.                 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true";  
  35.                 this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-";  
  36.             } 

收缩的时候,我们直接隐藏行就可以了.

收缩行

 
 
 
  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设置为false,标明该节点已经收缩  
  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

网站名称:玩转DataGridView之行的展开与收缩
URL分享:http://www.hantingmc.com/qtweb/news21/497371.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联