Devexpress使用之:TreeList控件

http://www.cnblogs.com/habin/archive/2009/04/15/1436418.html


Devexpress系列控件功能很强大,使用起来也不太容易,我也是边摸索边使用,如果有时间我会把常用控件的使用方法整理出来的。

 
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;

namespace TreeListDemo1
{
    public partial class Form1 : Form
    {
        private string[] rootNames;
        private string[] root = new string[] { "AAA", "BBB\\", "CCC", "DDD", "EEE" };

        public Form1()
        {
            InitializeComponent();

            //初始化TreeList控件
            CreateArrayWithNames();
            AppendNodes(rootNames, null, "", "");
        }

        #region 自定义函数
        //创建节点数组
        private void CreateArrayWithNames()
        {
            rootNames = new string[root.Length];
            for (int i = 0; i < root.Length; i++)
                rootNames[i] = root[i];
        }

        //追加节点
        private void AppendNodes(string[] names, TreeListNode aNode, string root, string root2)
        {
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            treeList1.BeginUnboundLoad();
            TreeListNode node = null;
            foreach (string s in names)
            {
                if (s.EndsWith("\\"))
                {

                    node = treeList1.AppendNode(new object[] { s.Substring(0, s.Length - 1) }, aNode);
                    aNode = node;

                }
                else
                {
                    node = treeList1.AppendNode(new object[] { s }, aNode);
                }

                node.Tag = true;

            }
            treeList1.EndUnboundLoad();

            Cursor.Current = currentCursor;
        }

        //获取节点名称
        private string FullNameByNode(TreeListNode node, int columnId)
        {
            string ret = node.GetValue(columnId).ToString();
            while (node.ParentNode != null)
            {
                node = node.ParentNode;
                ret = node.GetValue(columnId).ToString() + "\\" + ret;
            }
            return ret;
        }
#endregion

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            string s = FullNameByNode(e.Node, 0);
            this.Text = s;
        }
    }
}

 


你可能感兴趣的:(Devexpress使用之:TreeList控件)