Dev TreeList树控件使用

Winform 快速初始化Dev TreeList树控件的数据
使用Id,ParentId数据结构即可初始化TreeList,如下图:
Dev TreeList树控件使用_第1张图片
图中TreeList控件命名tlCategory。
产品类别数据表定义:
Dev TreeList树控件使用_第2张图片
建议Id,ParentId数据取名用01:CPU,0101:AMD-CPU,010101:AMD-XXCPU表示,虽然意义不是特别明显,顺势而为吧,群众认为是对的也不会太离谱,就是错了也能得到群众的支持。
实现代码:
C# Code:
tlCategory.KeyFieldName = "CategoryId";//设置主键 
tlCategory.ParentFieldName = "ParentId";//设置父级主键 
tlCategory.RootValue = "";//顶级树结点的值 
tlCategory.DataSource = _BllInstance.GetProductCategory();//绑定产品类别(树)的数据源 
//来源:C/S框架网(www.csframework.com) QQ:1980854898
最后,需要说明TreeList.Columns的配置:
在TreeList控件上点右键,选择Run Designer菜单,打开Property Editor窗体,Add一个Column, 设置FieldName为CategoryName用于显示数据。然后设置Caption,搞定。
Dev TreeList树控件使用_第3张图片

2、有实例参考。

代码:

#region TreeList填充
        public override void TreeListFillInfo()
        {
            try
            {
                DataTable treedata = CommonProxyFactory.GetData("B009").Tables[0];
                this.ByTreeList.Nodes.Clear();

                foreach (DataRow row in treedata.Rows)
                {
                    TreeListNode node = this.ByTreeList.AppendNode("id", null);
                    node.SetValue(GetTreeListCoumnName, row["category_name"].ToString());
                    node.Tag = row["category"].ToString();
                    TreeSon(node, row["category"].ToString());
                }
            }
            catch (Exception ex)
            { }
        }

        private void TreeSon(TreeListNode node, string ID)
        {
            try
            {
                DataTable tb = CommonProxyFactory.GetData("B010", new ParameterValue { Name = "@Filter", Value = "where main_category='" + ID + "'" + "and is_main =1" }).Tables[0];
                foreach (DataRow row in tb.Rows)
                {
                    TreeListNode sonnode = node.TreeList.AppendNode(tb, node);
                    sonnode.SetValue(GetTreeListCoumnName, row["main_module_name"].ToString());
                    sonnode.Tag = row["main_id"].ToString();
                }
            }
            catch (Exception ex)
            { }
        }
        #endregion

       //获取Treelist树形内column名称
        public string GetTreeListCoumnName
        {
            get { return this.ByTreeList.Columns[0].FieldName; }
        }
        
        //设置Treelist树形的标题名
        public string SetTreeListColumnCaption
        {
            set { this.ByTreeList.Columns[0].Caption = value; }
        }

 

你可能感兴趣的:(C#,WinForm)