DataGridView之行的展开与收缩
很多数据都有父节点与子节点,我们希望单击父节点的时候可以展开父节点下的子节点数据。
比如一个医院科室表,有父科室与子科室,点击父科室后,在父科室下面可以展现该科室下的所有子科室。
我们来说一下在DataGridView中如何实现这个功能。
首先,创建示例数据:

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,'','XXX医院')
insert into Department values('门诊内科',1,'','XXX医院')
insert into Department values('门诊手术',1,'','XXX医院')
insert into Department values('门诊儿科',1,'','XXX医院')
insert into Department values('神经内室',2,'','XXX医院')
insert into Department values('神经外科',2,'','XXX医院')
insert into Department values('住院手术',2,'','XXX医院')
insert into Department values('住院康复',2,'','XXX医院')

其实思路很简单,就是在展开父节点的时候,在父节点下插入新的DataGridViewRow;收缩父节点的时候,在父节点下删除该子节点的DataGridViewRow。
为了简便,代码中的数据读取我都直接硬编码了。
加载父节点数据,除了数据库中的列外我还新加了两列:IsEx与EX。

private void DataGridBing(DataTable table)
{
if (table.Rows.Count > )
{
for (int i = ; 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 > )
{
//插入行
this.dataGridView1.Rows.Insert(e.RowIndex+, table.Rows.Count);
for (int i = ; i < table.Rows.Count; i++)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex + i+];
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 > )
{
//利用Remove
for (int i = ; 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 > ; 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 > )
{
//插入行
this.dataGridView1.Rows.Insert(e.RowIndex + , table.Rows.Count);
for (int i = ; i < table.Rows.Count; i++)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex + i + ];
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 = ; 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 = ; 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之行的展开与收缩的更多相关文章
- ios知识点总结——UITableView的展开与收缩及横向Table
UITableVIew是iOS开发中使用最为广泛的一种控件,对于UITableView的基本用法本文不做探讨,本文主要是针对UITableView的展开与收缩进行阐述,在文章的后面也会探讨一下横向ta ...
- ios-tableViewcell展开与收缩动画处理
[前言] 在使用华尔街见闻 app 时,看到它的 tableVeiw 上的 cell 具有很好的展开与收缩功能.于是自己想了一下实现,感觉应该挺简单的,于是心痒痒写个 demo 实现一波.华尔街见闻 ...
- Ext.grid rowexpander的展开与收缩
这里写Ext.grid.Panel的展开与收缩. 1. 确保在grid存在rowexpander对象: plugins: [{ ptype: 'rowexpander', rowBodyTpl: [' ...
- C# DataGridView显示行号的三种方法
方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dgGrid_RowPostPaint( obj ...
- DataGridView显示行号的几种方法来自http://www.soaspx.com/dotnet/csharp/csharp_20100204_2740.html
方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dataGridView1_RowPostPai ...
- 【转】DataGridView显示行号
ref:http://blog.csdn.net/xieyufei/article/details/9769631 方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件 ...
- dhtmlxtree 节点 展开收缩:新增了直接点 文本内容 也 实现了 展开收缩 功能(并记住了展开、收缩状态)
dhtmlxtree 节点 展开收缩通常情况我们按 +- 就实现了 展开收缩 功能,为了方便我们新增了直接点 文本内容 也 实现了 展开收缩 功能(并记住了展开.收缩状态) tree = new dh ...
- js 实现内容的展开和收缩
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Winform中的DatagridView显示行号
1.设置 RowPostPaint 为true 2.启用RowPostPaint事件 /// <summary> /// DataGridView显示行号 /// </summary ...
随机推荐
- noip2006T1 能量项链
最近一直在做noip的题,由于太水一直没有写题解,可是这道题,这道题.啊啊啊啊…… 明明水的要命,调了一节课啊!!我干脆不要搞OI了啊! 特别逗比的错误啊!!!! 代码: #include <c ...
- Virtualizing WrapPanel VS toolkit:WrapPanel
用toolkit:WrapPanel的时候,LIST太大,内存不行,等下我试试 Virtualizing WrapPanel这个 http://www.codeproject.com/Articles ...
- matlab之图像处理(2)
diagram = imread('lena1.png') diagram = rgb2gray(diagram);%------------------------------将图片转换为灰度图 N ...
- Silverlight自动根据屏幕分辨率进行布局
xaml: <UserControl x:Class="SLCenterLayout.MainPage" xmlns="http://schemas.microso ...
- curl 报错记录,mark
今天在做接口开发的时候,使用curl post ,请求返回数据为 null ,很纳闷,然后使用 curl_errno 打印出来的错误代码为 28 ,curl_error($ch) 打印出来的是Oper ...
- Redis源码研究--redis.h
------------7月3日------------ /* The redisOp structure defines a Redis Operation, that is an instance ...
- Android NDK环境配置
之前做了一个基于ffmpeg的软解播放器,熟悉了NDK开发的配置环境过程,但是由于太忙一直没有时间写笔记. 首先,介绍一下在这里所参与协作的软件包: 1. JDK: 这个软件被Eclipse依赖. 2 ...
- Java中如何防止内存泄漏的发生
在Java开发中我们常常会遇到内存泄漏的情况发生.那么为什么会发生内存泄漏,以及怎样去防止! 内存泄漏的定义:对象已经没有被应用程序使用,但是垃圾回收器没办法移除它们,因为还在被引用着. 为什么会发生 ...
- thinkphp对数据库操作有哪些内置函数
getModelName() 获取当前Model的名称 getTableName() 获取当前Model的数据表名称 switchModel(type,vars=array()) 动态切换模型 tab ...
- Payment Terms 收付款条件和分期付款设置
SAP Payment Terms 中文翻译为收付款条件,他的用途是应收和应付的财务凭证中账期的管理,顾名思义即手动录入和自动生成的财务文档多少天内冲销处理则为正常,否则为超期应收应付文档,它包含的内 ...