目录
  • 关于 FlyTreeView
  • DataSet 数据准备
  • 范例运行环境
  • 方法设计
  • 代码实现
    • 方法代码
    • 调用示例
  • 小结

    关于 FlyTreeView

    NineRays.WebControls.FlyTreeView 是 9rays.net 推出的一款功能强大的树状模型数据显示控件,本文将介绍使用其 Asp.net 版本控件,并结合 DataSet 对象进行数据显示。

    显示效果如下图:

    C# DataSet结合FlyTreeView实现显示树状模型数据

    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,并引用,如下代码:

    C# DataSet结合FlyTreeView实现显示树状模型数据

    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 更多操作这里不再做进一步说明,请参考其官网说明。

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。