DataGridView展开与收缩功能实现
很多数据都有父节点与子节点,我们希望单击父节点的时候可以展开父节点下的子节点数据。
比如一个医院科室表,有父科室与子科室,点击父科室后,在父科室下面可以展现该科室下的所有子科室。
我们来说一下在DataGridView中如何实现这个功能。
首先,创建示例数据:
示例数据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医院') insert into Department values('门诊内科',1,'2222','XXX医院') insert into Department values('门诊手术',1,'3333','XXX医院') insert into Department values('门诊儿科',1,'4444','XXX医院') insert into Department values('神经内室',2,'5555','XXX医院') insert into Department values('神经外科',2,'6666','XXX医院') insert into Department values('住院手术',2,'7777','XXX医院') insert into Department values('住院康复',2,'8888','XXX医院')
其实思路很简单,就是在展开父节点的时候,在父节点下插入新的DataGridViewRow;收缩父节点的时候,在父节点下删除该子节点的DataGridViewRow。
为了简便,代码中的数据读取我都直接硬编码了。
加载父节点数据,除了数据库中的列外我还新加了两列: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"]; //用于显示该行是否已经展开 row.Cells["IsEx"].Value = "false"; //用于显示展开或收缩符号,为了简单我就直接用字符串了,其实用图片比较美观 row.Cells["EX"].Value = "+"; } } }
下面就是Cell的单击事件了,分别在事件中写展开的插入与收缩的删除.
插入子节点:
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设置为true,标明该节点已经展开 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") { 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设置为false,标明该节点已经收缩 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false"; this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+"; }
这里面通过比较ID来唯一确定一行,循环比较多,因为子节点是紧接着父节点的,我们可以确定子节点所在的行数,所以用RemoveAt()方法更好。
//利用RemoveAt for (int i = table.Rows.Count; i > 0; i--) { //删除行 this.dataGridView1.Rows.RemoveAt(i + e.RowIndex); }
上面的做法是通过不断的插入与删除来实现,但这样与数据库的交互变得很频繁。更好的做法应该是插入一次,然后通过隐藏或显示行来实现我们的效果。
为此,我们还要在grid中新增两个列:
IsInsert:用来判断该行是否已经有插入子节点数据
RowCount:用来保存该行下插入的子节点数量。
在方法DataGridBing中,绑定数据时,应该再加一列:
//是否插入 row.Cells["IsInsert"].Value = "false";
而在增加节点的时候,我们要多做一个判断,如果IsInsert为false就插入数据,如果为true就显示数据
展开行
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 { //显示数据 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设置为true,标明该节点已经展开 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设置为false,标明该节点已经收缩 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false"; this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+"; }
大家知道DataGridView是如何实现展开收缩的吧,希望大家不仅知道是如何实现的还要动手实验一番,才不枉小编辛苦整理此文章哦
相关文章
- 这篇文章主要介绍了C#实现3步手动建DataGridView的方法,实例分析了C#实现手动创建DataGridView的原理与技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C#中DataGridView动态添加行及添加列的方法,涉及C#中DataGridView针对行与列动态操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
C#中datagridview使用tooltip控件显示单元格内容的方法
这篇文章主要介绍了C#中datagridview使用tooltip控件显示单元格内容的方法,实例分析了C#控件的相关使用技巧,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了基于jQuery实现收缩展开功能的相关资料,需要的朋友可以参考下...2016-03-22
- 这篇文章主要介绍了C#中DataGridView常用操作,以实例形式总结了DataGridView绑定下拉列表、设置默认值、判断复选框是否选中等技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 这个例子的功能是c#读取excel文件,大家可以参考使用...2020-06-25
实现DataGridView控件中CheckBox列的使用实例
最近做WindowsForms程序,使用DataGridView控件时,加了一列做选择用,发现CheckBox不能选中。搜索后,要实现DataGridView的CellContentClick事件,将代码贴一下...2021-09-22- 这篇文章主要介绍了c#中datagridview处理非绑定列的方法,实例分析了C#中datagridview的使用技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了vue-列表下详情的展开与折叠案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-29
- 这篇文章主要介绍了C#中改变DataGridView控件边框颜色的方法,默认的DataGridView边框颜色很丑,本文用编程方法实现修改DataGridView边框颜色,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C#中DataGridView的样式设置方法,包括交替行颜色、单元格内容有效性检查、单元格的选择模式等,需要的朋友可以参考下...2020-06-25
- 下在我们来看一篇关于PHP7扩展开发之传参与返回值的例子,希望这一篇文章能够帮助到各位朋友,具体的细节如下文介绍. 前言 这次,我们将演示如何在PHP扩展中接受传入的...2016-11-25
- 本文主要介绍了原生js实现网页顶部自动下拉收缩广告效果的实例代码。具有一定的参考价值,下面跟着小编一起来看下吧...2017-01-23
C# 实现dataGridView选中一行右键出现菜单的示例代码
这篇文章主要介绍了C# 实现dataGridView选中一行右键出现菜单,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-03- 一般情况下,SQL数据库的收缩并不能很大程度上减小数据库大小,其主要作用是收缩日志大小,应当定期进行此操作以免数据库日志过大。 1、设置数据库模式为简单模...2016-11-25
- DataGridView中CheckBox实现某一列单选,需要的朋友可以参考一下...2021-09-22
- WinForm程序中表单的列可自定义显示及隐藏,是一种常见的功能,对于用户体验来说是非常好的。这篇文章主要介绍了DataGridView右键菜单自定义显示及隐藏列功能,需要的朋友可以参考下...2021-09-22
- c#读取xml文件到datagridview实例,需要的朋友可以参考一下...2020-06-25
- 这个功能的GridView多层嵌套没有问题,因为已经做了无限次数,但是折叠与展开的功能,却花上不少时间(网上找资料),虽找到资料可参考,还是了解明它,并修改适合自己程序使用,感兴趣的朋友可以了解下...2021-09-22
- 这篇文章主要为大家详细介绍了DataGridView带图标的单元格的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25