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

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

我们来说一下在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,'','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之行的展开与收缩的更多相关文章

  1. ios知识点总结——UITableView的展开与收缩及横向Table

    UITableVIew是iOS开发中使用最为广泛的一种控件,对于UITableView的基本用法本文不做探讨,本文主要是针对UITableView的展开与收缩进行阐述,在文章的后面也会探讨一下横向ta ...

  2. ios-tableViewcell展开与收缩动画处理

    [前言] 在使用华尔街见闻 app 时,看到它的 tableVeiw 上的 cell 具有很好的展开与收缩功能.于是自己想了一下实现,感觉应该挺简单的,于是心痒痒写个 demo 实现一波.华尔街见闻 ...

  3. Ext.grid rowexpander的展开与收缩

    这里写Ext.grid.Panel的展开与收缩. 1. 确保在grid存在rowexpander对象: plugins: [{ ptype: 'rowexpander', rowBodyTpl: [' ...

  4. C# DataGridView显示行号的三种方法

    方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dgGrid_RowPostPaint( obj ...

  5. DataGridView显示行号的几种方法来自http://www.soaspx.com/dotnet/csharp/csharp_20100204_2740.html

    方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dataGridView1_RowPostPai ...

  6. 【转】DataGridView显示行号

    ref:http://blog.csdn.net/xieyufei/article/details/9769631 方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件 ...

  7. dhtmlxtree 节点 展开收缩:新增了直接点 文本内容 也 实现了 展开收缩 功能(并记住了展开、收缩状态)

    dhtmlxtree 节点 展开收缩通常情况我们按 +- 就实现了 展开收缩 功能,为了方便我们新增了直接点 文本内容 也 实现了 展开收缩 功能(并记住了展开.收缩状态) tree = new dh ...

  8. js 实现内容的展开和收缩

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. Winform中的DatagridView显示行号

    1.设置 RowPostPaint 为true 2.启用RowPostPaint事件 /// <summary> /// DataGridView显示行号 /// </summary ...

随机推荐

  1. activiti系列导读

    此用于管理activiti系列标签文章,activiti的分析是建立在目前最新的版本5.21之上. 官方指导手册链接:http://www.activiti.org/userguide/index.h ...

  2. ADO.NET笔记——将DataReader作为函数返回值

    相关知识: 在很多情况下,可能把数据库的访问封装到一个函数中,通过该函数返回一个DataReader对象给调用者.例如定义函数:SqlDataReader returnDR(),然后再Main函数中调 ...

  3. Factory_Method

    class Product { public: virtual ~Product() {} ; }; class ProductA : public Product { public: Product ...

  4. arduino入门学习实现语音控制LED灯

    需要的准备的硬件arduino+PC+麦克风实现语音命令控制LED灯的亮灭. 首先需要将写好的arduino程序烧录到arduino uno主板中,下面是代码如下: int val;//定义变量val ...

  5. linq 日常关键字使用

    1.from var scoreQuery = from student in students from score in student.Scores where score > 90 se ...

  6. eclipse juno版本中没用 ant

    下载了谷歌提供的Android集成开发工具ADT,里面封装了Eclipse,但是很奇怪的是竟然没有Ant插件在里面 标准的Eclipse一般都是内置集成了Ant的. 然后到eclipse的plugin ...

  7. How to modify Code Comments[AX2012]

    // This is a framework class. Customizing this class may cause problems with future upgrades to the ...

  8. Eclipse常用功能

    功能(Functions):内置运行java程序的插件(JDT:java develop tools)工作集(WorkSet)(Task list)计划任务管理器(Mylyn)系统配置(Prefere ...

  9. html笔记 仅适用于个人

    如何使图片与文本框上下对齐 其实就给<img>加一个属性 align="absmiddle" <form method="post" acti ...

  10. 【个人笔记】003-PHP基础-01-PHP快速入门-03-PHP环境搭建

    003-PHP基础-01-PHP快速入门 03-PHP环境搭建 1.客户端(浏览器) IE FireFox CHROME Opera Safari 2.服务器 是运行网站的基本 是放置程序代码的地方 ...