- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
- [C#] Hướng dẫn Copy With All Property in Class Object
- [DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView
- [C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
[DEVEXPRESS] Hướng dẫn sử dụng TreeListLookupEdit và lưu trạng thái đóng mở từng Node C#
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách sử dụng TreeListLookupEdit Devexpress C#, và lưu lại trạng thái đóng mở của từng Node.
[DEVEXPRESS] Hướng dẫn sử dụng TreeListLookupEdit và lưu trạng thái đóng mở từng Node C#
Trong bài viết trước, mình đã có hướng dẫn các bạn cách làm việc với TreeList Devepress, bài này mình sẽ nói về TreeListLookupEdit.
Các bạn có thể tham khảo thêm về TreeList Devexpress C#
Dưới đây là giao diện demo ứng dụng sử dụng TreeListLookupEdit Devexpress C#:
TreeListLookupEdit C# thường được sử dụng để hiển thị các loại như: Danh mục, phân quyền người dùng, theo sơ đồ cây...
Trong bài viết này ngoài chức năng hiển thị mình hỗ trợ thêm các chức năng sau:
- Expand all node
- Collapse all node
- Save States Node sang File XML
- Retore States Node
Chức năng Save State của TreeListLookupEdit là gì?
Như các bạn nhìn hình ở trên có rất nhiều danh mục đang được chúng ta mở ra. Và mình mong muốn là sau khi mở ra và đóng ứng dụng lại.
Thì lần sau, khi mở vào TreeList này, vẫn sẽ hiển thị lại trạng thái như cũ mình đã chọn.
Để lưu trữ trạng thái của Node Expand hay Collapse trên TreeListLookupEdit, các bạn tạo cho mình 1 class TreeListViewState.cs
Source code TreeListViewState.cs
C#
using System;
using System.Collections;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Nodes.Operations;
using System.Xml.Linq;
using System.Windows.Forms;
namespace TreeListLookupEdit
{
class TreeListViewState
{
private ArrayList expanded;
private ArrayList selected;
private object focused;
private int topIndex;
public TreeListViewState() : this(null) { }
public TreeListViewState(TreeList treeList)
{
this.treeList = treeList;
expanded = new ArrayList();
selected = new ArrayList();
}
public void Clear()
{
expanded.Clear();
selected.Clear();
focused = null;
topIndex = 0;
}
private ArrayList GetExpanded()
{
OperationSaveExpanded op = new OperationSaveExpanded();
TreeList.NodesIterator.DoOperation(op);
return op.Nodes;
}
private ArrayList GetSelected()
{
ArrayList al = new ArrayList();
foreach (TreeListNode node in TreeList.Selection)
al.Add(node.GetValue(TreeList.KeyFieldName));
return al;
}
string fileName = "treeListState.xml";
public void LoadState()
{
try
{
XDocument xmlDocument = XDocument.Load(fileName);
foreach (XElement el in xmlDocument.Root.Elements())
{
switch (el.Name.LocalName)
{
case "NodeData":
focused = Convert.ToDouble(el.Attribute("Focused").Value);
topIndex = Convert.ToInt32(el.Attribute("Top").Value);
break;
case "SelectedNodes":
selected.Clear();
foreach (var item in el.Attribute("IDs").Value.Split(','))
selected.Add(Convert.ToDouble(item));
break;
case "ExpandedNodes":
expanded.Clear();
foreach (var item in el.Attribute("IDs").Value.Split(','))
expanded.Add(Convert.ToDouble(item));
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show("No data. Check an XML file");
return;
}
TreeList.BeginUpdate();
try
{
TreeList.CollapseAll();
TreeListNode node;
foreach (object key in expanded)
{
//node = TreeList.FindNodeByKeyID(key);
node = TreeList.FindNode(n => n.GetValue("ID").ToString() == key.ToString());
if (node != null)
node.Expanded = true;
}
foreach (object key in selected)
{
node = TreeList.FindNode(n => n.GetValue("ID").ToString() == key.ToString());
if (node != null)
TreeList.Selection.Add(node);
}
TreeList.FocusedNode = TreeList.FindNode(n => n.GetValue("ID").ToString() == focused.ToString());
}
finally
{
TreeList.EndUpdate();
TreeList.TopVisibleNodeIndex = TreeList.GetVisibleIndexByNode(TreeList.FocusedNode) - topIndex;
}
}
public void SaveState()
{
//if (TreeList.FocusedNode != null)
//{
expanded = GetExpanded();
selected = GetSelected();
focused = TreeList.FocusedNode[TreeList.KeyFieldName];
topIndex = TreeList.GetVisibleIndexByNode(TreeList.FocusedNode) - TreeList.TopVisibleNodeIndex;
XDocument xmlDocument = new XDocument(new XElement("TreeListState"));
xmlDocument.Root.Add(
new XElement("NodeData",
new XAttribute("Focused", focused),
new XAttribute("Top", topIndex)));
var selectedNodes = string.Join(",", selected.ToArray());
var expandedNodes = string.Join(",", expanded.ToArray());
xmlDocument.Root.Add(
new XElement("SelectedNodes",
new XAttribute("IDs", selectedNodes)));
xmlDocument.Root.Add(
new XElement("ExpandedNodes",
new XAttribute("IDs", expandedNodes)));
xmlDocument.Save(fileName);
//}
//else
// Clear();
}
private TreeList treeList;
public TreeList TreeList
{
get
{
return treeList;
}
set
{
treeList = value;
Clear();
}
}
class OperationSaveExpanded : TreeListOperation
{
private ArrayList al = new ArrayList();
public override void Execute(TreeListNode node)
{
if (node.HasChildren && node.Expanded)
al.Add(node.GetValue(node.TreeList.KeyFieldName));
}
public ArrayList Nodes { get { return al; } }
}
}
}
Và source code Form1.cs
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TreeListLookupEdit
{
public partial class Form1 : XtraForm
{
public Form1()
{
InitializeComponent();
treeListViewState = new TreeListViewState(treeListLookUpEdit1TreeList);
}
TreeListViewState treeListViewState;
private void Form1_Load(object sender, EventArgs e)
{
string json_danhmuc_muaban = File.ReadAllText("data.json");
var table = (DataTable)JsonConvert.DeserializeObject(json_danhmuc_muaban, (typeof(DataTable)));
DataTable tbl_danhmuc_muaban = table.AsEnumerable()
.CopyToDataTable();
treeListLookUpEdit1.Properties.AutoExpandAllNodes = false;
treeListLookUpEdit1.Properties.DataSource = tbl_danhmuc_muaban;
treeListLookUpEdit1.Properties.ValueMember = "ID";
treeListLookUpEdit1.Properties.DisplayMember = "name";
treeListLookUpEdit1TreeList.StateImageList = imageList1;
treeListLookUpEdit1TreeList.ViewStyle = DevExpress.XtraTreeList.TreeListViewStyle.TreeView;
treeListLookUpEdit1TreeList.OptionsView.ShowTreeLines = DevExpress.Utils.DefaultBoolean.True;
treeListLookUpEdit1TreeList.CollapseAll();
}
private void treeListLookUpEdit1TreeList_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
{
try
{
int index = int.Parse(e.Node.GetValue("img_index").ToString());
if (index >= 0)
{
e.NodeImageIndex = index;
}
}
catch (Exception) { }
}
public bool isExpand = false;
private void btn_expand_Click(object sender, EventArgs e)
{
isExpand = !isExpand;
if (isExpand)
{
treeListLookUpEdit1TreeList.ExpandAll();
btn_expand.Text = "Collapse All";
}
else
{
treeListLookUpEdit1TreeList.CollapseAll();
btn_expand.Text = "Expand All";
}
}
private void btn_save_Click(object sender, EventArgs e)
{
treeListViewState.SaveState();
}
private void btn_restore_Click(object sender, EventArgs e)
{
treeListViewState.LoadState();
}
}
}
Các bạn có thể download source code ở bên dưới về để tham khảo.
Thanks for watching!