关于 flytreeview
ninerays.webcontrols.flytreeview 是 9rays.net 推出的一款功能强大的树状模型数据显示控件,本文将介绍使用其 asp.net 版本控件,并结合 dataset 对象进行数据显示。
显示效果如下图:

dataset 数据准备
我们在 ms sql server 创建 groupusers(群组用户表),其结构如下表:
| 序号 | 字段名 | 类型 | 说明 |
|---|---|---|---|
| 1 | cid | uniqueidentifier | 唯一标识 |
| 2 | group_cid | uniqueidentifier | 所属群组id标识(引用群组表groups) |
| 3 | account_cid | uniqueidentifier | 人员帐户id(引用用户表accounts,用于显示昵称、姓名等) |
| 4 | parent_cid | uniqueidentifier | 父结点id,所属管理者id |
| 5 | sortcode | int | 同级排序号 |
| 6 | sys_insuser | nvarchar(100) | 创建者用户名 |
| 7 | sys_instime | datetime | 创建时间 |
| 8 | sys_upduser | nvarchar(100) | 最后修改者用户名 |
| 9 | sys_updtime | datetime | 最后修改时间 |
该表所涉及的引用表这里不在赘述,我们假设有如下 sql 语句:
select a.cid,a.parent_cid,nickname+'('+name+')' truename
from groupusers a,accounts b
where a.group_cid=@group_cid
and a.account_cid=b.cid
我们需要重点得到 cid(唯一标识)、parent_cid(父节点id) 和 truename (显示名称) 三个字段。查询并写入到 dataset 中。
涉及表结构创建脚本
群组用户表
set ansi_nulls on
go
set quoted_identifier on
go
create table [dbo].[groupusers](
[cid] [uniqueidentifier] rowguidcol not null,
[group_cid] [uniqueidentifier] not null,
[account_cid] [uniqueidentifier] not null,
[parent_cid] [uniqueidentifier] null,
[sortcode] [int] null,
[sys_insuser] [nvarchar](100) null,
[sys_instime] [datetime] null,
[sys_upduser] [nvarchar](100) null,
[sys_updtime] [datetime] null,
constraint [pk_groupusers] primary key clustered
(
[cid] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary],
constraint [ix_cc_groupusers] unique nonclustered
(
[group_cid] asc,
[parent_cid] asc,
[account_cid] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go
alter table [dbo].[groupusers] add constraint [df_groupusers_cid] default (newid()) for [cid]
go
用户表
set ansi_nulls on
go
set quoted_identifier on
go
create table [dbo].[accounts](
[cid] [uniqueidentifier] rowguidcol not null,
[name] [nvarchar](50) null,
[nickname] [nvarchar](500) null
)go
alter table [dbo].[accounts] add constraint [df_accounts_cid] default (newid()) for [cid]
go
范例运行环境
操作系统: windows server 2019 datacenter
数据库:microsoft sql server 2016
.net版本: .netframework4.0 或以上
开发工具:vs2019 c#
方法设计
simpletreeview方法返回结点总数,其参数说明见下表:
| 序号 | 参数名 | 类型 | 说明 |
|---|---|---|---|
| 1 | tv | flytreenodecollection | 传入的flytreeview的当前结点集合对象 |
| 2 | ds | dataset | 数据集对象,默认只取tables[0] |
| 3 | key | string | 数据表的唯一标识字段名 |
| 4 | parentkey | string | 数据表的父结点字段名 |
| 5 | dis | string | 数据表的显示名称字段名 |
| 6 | keytype | string | 标识类型,这是我们自定的规范,比如cid(字符)、id(数值)固定名称的处理方式,默认处理方式对key或parentkey进行字符串过滤处理 |
| 7 | initvalue | string | 是否指定一个初始值 |
| 8 | firstlevel | bool | 是否指遍历一级,如果为true,则不在进行递归 |
| 9 | initbykey | bool | 初始值使用哪个关键字段,false使用父节点,true使用唯一标识,默认为false |
代码实现
方法代码
int simpletreeview(fwebcontrols.flytreenodecollection tv, dataset ds, string key, string parentkey, string dis, string keytype, string initvalue, bool firstlevel,bool initbykey)
{
int rv = 0;
dataview dv = new dataview();
dv.table = ds.tables[0];
fwebcontrols.flytreenode tmpnd;
switch (keytype)
{
case "cid": dv.rowfilter = initvalue == "" ? " " + (initbykey == false ? parentkey : key) + " is null " : " " + (initbykey == false ? parentkey : key) + "='" + initvalue + "'"; break;
case "id": dv.rowfilter = initvalue == "" ? " " + (initbykey == false ? parentkey : key) + " is null " : "" + (initbykey == false ? parentkey : key) + "=" + initvalue + ""; break;
default: dv.rowfilter = "isnull(" + (initbykey == false ? parentkey : key) + ",'')='" + initvalue + "'"; break;
}
rv = dv.count;
foreach (datarowview drv in dv)
{
tmpnd = new fwebcontrols.flytreenode();
tmpnd.text = drv[dis].tostring();
tmpnd.value = drv[key].tostring();
tv.add(tmpnd);
if (!firstlevel)
simpletreeview(tmpnd.childnodes, ds, key, parentkey, dis, keytype, tmpnd.value,firstlevel,false);
}
return rv;
}调用示例
我们首先需要在页面注册控件,代码如下:
<%@ register assembly="microsoft.web.ui.webcontrols" namespace="microsoft.web.ui.webcontrols" tagprefix="codn" %>
前端代码如下:
<ninerays:flytreeview id="usertree" expandlevel="4" width="100%" cssclass="form-control" runat="server" style="padding-top: 10px; padding-bottom: 10px; height: 400px;" canbeselected="true" backcolor="white"
postbackonselect="false"
enabletheming="true" imageset="office2003" imagesetcustompath="/images/vista/"
contentclicktogglescheckbox="true" ischeckbox="true">
<selectedstyle backcolor="#bfcfff" />
</ninerays:flytreeview>
后端调用代码如下:
usertree.nodes.clear(); object ds=initdataset(); string initvalue=""; if(rowscount==0) return 0; simpletreeview(usertree.nodes, (dataset)ds, "cid", "parent_cid", "truename", "cid","", false, initvalue != "" ? true : false); usertree.contentclickcollapses = true; usertree.contentclickexpands = true;
小结
1、示例代码中如何获取 dataset 数据需要我们自行进行编写代码处理,这里只是抽象展示。
2、在 vs 中开发我们需要在 ide环境解决方案中添加此 dll,并引用,如下代码:

3、提供一个后端辅助方法 getflytreeviewallnodes,可以获得flytreeview的所有结点信息,并放置到 arraylist 中。
其参数说明见下表:
| 序号 | 参数名 | 类型 | 说明 |
|---|---|---|---|
| 1 | tv | flytreenodecollection | 要遍历的treeview集合 |
| 2 | rv2 | arraylist | 要存储的 arraylist 变量 |
方法代码如下:
public void getflytreeviewallnodes(fwebcontrols.flytreenodecollection tv, arraylist rv2)
{
for (int i = 0; i < tv.count; i++)
{
rv2.add(tv[i].value);
getflytreeviewallnodes(tv[i].childnodes, rv2);
}
}
方法会在指定的 arraylist 里存储 treeview 的 value 值 。
关于 ninerays.webcontrols.flytreeview 更多操作这里不再做进一步说明,请参考其官网说明。
到此这篇关于c# dataset结合flytreeview实现显示树状模型数据的文章就介绍到这了,更多相关c# dataset显示树状模型数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论