NEWS

[DEVEXPRESS] Hướng dẫn sử dụng TreeList C# để hiển thị danh sách theo dạng cây thư mục

[DEVEXPRESS] Hướng dẫn sử dụng TreeList C# để hiển thị danh sách theo dạng cây thư mục
Đăng bởi: Thảo Meo - Lượt xem: 21782 15:23:43, 14/06/2018C#   In bài viết

Trong các bài viết trước về Devexpress, mình đã có nhiều bài hướng dẫn các bạn sử dụng GridView, nhưng muốn các bạn muốn hiển thị danh sách theo dạng cây, thì TreeList Devexpress C# sẽ giúp bạn thực hiện dễ dàng.

[C#] TUTORIAL TREELIST IN DEVEXPRESS WINFORM

Khi sử dụng TreeList vào ứng dụng thì mình sẽ nhìn trực quan hơn.

TreeList thường được sử dụng nhiều trong các ứng dụng như: Explorer, cây tài khoản kế toán.

Dưới đây, giao diện sử dụng TreeList C#:

 

tutorial treelist in devexpress

 

- Để danh sách dữ liệu các bạn hiển thị theo dạng cây, mình sẽ phải sử dụng coloum để phân chia cấp dữ liệu:

1. ParentID

2. ID

Các bạn theo dõi bảng Database của mình bên dưới:

 

sử dụng treelist devexpress

 

- Mỗi dòng đều có 1 parent ID, những cột nào có chung Parent ID sẽ thành một nhóm (group).

+ Tiếp theo, các bạn cấu hình cho component TreeList 2 thuộc tính ParentFieldName KeyFieldName, để cho nó biết được đâu là node cha và đâu là khóa.

Các bạn set 2 thuộc tính bên bên tab Properties nhé.

ParentFieldName  = ParentID

KeyFieldName = Id

- Trong bài viết, mình sử dụng đọc dữ liệu từ file EmployeesGroups.xml, file data demo của Devexpress.

Nếu các bạn muốn xem bảng dữ liệu thì đặt Debug, và xem bảng chi tiết ở dataset nhé.

Trong ví dụ, mình cũng có hướng dẫn thêm cách chèn image theo cấp vào từng nhánh của Treelist ( Sử dụng ImageCollection)

Cái này, mình sẽ viết trong sự kiện GetStateImage với code C# như sau:

 

 private void treeList1_GetStateImage(object sender, GetStateImageEventArgs e)
        {
            string[] groupNames = new string[] { "Administration", "Inventory", "Manufacturing", "Quality", "Research", "Sales" };
            currentGroupName = (string)e.Node.GetValue("GroupName");
            e.NodeImageIndex = Array.FindIndex(groupNames, new Predicate(IsCurrentGroupName));
        }

 

- Dưới đây, là full source code demo Treelist Devexpress trên cho các bạn tham khảo:

 

using DevExpress.XtraTreeList;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeListDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string DBFileName = DevExpress.Utils.FilesHelper.FindingFileName(Application.StartupPath, "EmployeesGroups.xml");
            if (DBFileName != "")
            {
                DataSet dataSet = new DataSet();
                dataSet.ReadXml(DBFileName);
                treeList1.DataSource = dataSet.Tables[0].DefaultView;
                treeList1.ForceInitialize();
                treeList1.ExpandAll();
                treeList1.BestFitColumns();
            }

        }
        string currentGroupName;

        private bool IsCurrentGroupName(string groupName)
        {
            return currentGroupName.Contains(groupName);
        }



        private void treeList1_GetStateImage(object sender, GetStateImageEventArgs e)
        {
            string[] groupNames = new string[] { "Administration", "Inventory", "Manufacturing", "Quality", "Research", "Sales" };
            currentGroupName = (string)e.Node.GetValue("GroupName");
            e.NodeImageIndex = Array.FindIndex(groupNames, new Predicate(IsCurrentGroupName));
        }


    }
}

Bạn nào thực hiện không được, có thể download source code ở bên dưới để tham khảo nhé.

Happy Coding :)

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn sử dụng TreeList C# để hiển thị danh sách theo dạng cây thư mục
Đăng bởi: Thảo Meo - Lượt xem: 21782 15:23:43, 14/06/2018C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.