DEV系列--treeList用法

数据库读取数据显示到treeList中

效果图:



数据库表的设计图:


源代码:

using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Linq;  
using System.Windows.Forms;  
using DevExpress.XtraEditors;  
using System.Data.SqlClient;  
using DevExpress.XtraTreeList.Nodes;  
  
namespace lianxi  
{  
    public partial class treelist_shijian : DevExpress.XtraEditors.XtraForm  
    {  
        public treelist_shijian()  
        {  
            InitializeComponent();  
            ShuJu();  
        }  
        //读取数据库绑定到treeList1中  
        public void ShuJu()  
        {  
            SqlConnection conn = new SqlConnection("server=(local);database=DEV_Test;Integrated Security=true;");  
              
            SqlCommand comm = new SqlCommand("select * from Table_treelist", conn);//select后面要写“*”否则树形结构的效果出不来  
             conn.Open();  
            SqlDataReader reader = comm.ExecuteReader();  
            DataTable dt = new DataTable();  
            dt.Load(reader);  
            this.treeList1.DataSource = dt;  
              
            this.treeList1.KeyFieldName = "编号";  
            treeList1.ParentFieldName = "父类编号";  
            treeList1.Columns[0].Caption = "公司";  
            conn.Close();  
  
            //显示复选框  
            treeList1.OptionsView.ShowCheckBoxes = true;  
        }  
  
        //选择某一节点时,该节点的子节点全部选择  取消某一节点时,该节点的子节点全部取消选择  
        private void SetCheckedChildNodes(TreeListNode node, CheckState check)  
        {  
            for (int i = 0; i < node.Nodes.Count; i++)  
            {  
                node.Nodes[i].CheckState = check;  
                SetCheckedChildNodes(node.Nodes[i], check);  
            }  
        }  
  
          
        // 某节点的子节点全部选择时,该节点选择   某节点的子节点未全部选择时,该节点不选择  
        private void SetCheckedParentNodes(TreeListNode node, CheckState check)  
        {  
            if (node.ParentNode != null)  
            {  
  
                CheckState parentCheckState = node.ParentNode.CheckState;  
                CheckState nodeCheckState;  
                for (int i = 0; i < node.ParentNode.Nodes.Count; i++)  
                {  
                    nodeCheckState = (CheckState)node.ParentNode.Nodes[i].CheckState;  
                    if (!check.Equals(nodeCheckState))//只要任意一个与其选中状态不一样即父节点状态不全选  
                    {  
                        parentCheckState = CheckState.Unchecked;  
                        break;  
                    }  
                    parentCheckState = check;//否则(该节点的兄弟节点选中状态都相同),则父节点选中状态为该节点的选中状态  
                }  
  
                node.ParentNode.CheckState = parentCheckState;  
                SetCheckedParentNodes(node.ParentNode, check);//遍历上级节点  
            }  
        }  
  
        //触发选择节点事件  
        private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)  
        {  
            SetCheckedChildNodes(e.Node, e.Node.CheckState);  
            SetCheckedParentNodes(e.Node, e.Node.CheckState);  
        }  
  
        private List<string> lstCheckedOfficeID = new List<string>();//选择局ID集合  
          
        // 获取选择状态的数据主键ID集合  
        private void GetCheckedOfficeID(TreeListNode parentNode)  
        {  
            if (parentNode.Nodes.Count == 0)  
            {  
                return;//递归终止  
            }  
  
            foreach (TreeListNode node in parentNode.Nodes)  
            {  
                if (node.CheckState == CheckState.Checked)  
                {  
                    DataRowView drv = treeList1.GetDataRecordByNode(node) as DataRowView;  
                    if (drv != null)  
                    {  
                        string OfficeID = (string)drv["名称"];  
                        lstCheckedOfficeID.Add(OfficeID);  
                    }  
  
                }  
                GetCheckedOfficeID(node);  
            }  
        }  
  
        //查看哪些节点被选中  
        private void simpleButton1_Click(object sender, EventArgs e)  
        {  
            this.lstCheckedOfficeID.Clear();  
  
            if (treeList1.Nodes.Count > 0)  
            {  
                foreach (TreeListNode root in treeList1.Nodes)  
                {  
                    GetCheckedOfficeID(root);  
                }  
            }  
  
            string idStr = string.Empty;  
            foreach (string id in lstCheckedOfficeID)  
            {  
                idStr += id + " ";  
            }  
            MessageBox.Show(idStr);  
        }  
          
    }  
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值