mb61ab431c11928 2022-01-26 12:50:08 阅读数:542
Source code :
<span style="font-size:24px;">using System;
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();
}
// Bind the read database to treeList1 in
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 Write later “” Otherwise, the effect of tree structure will not come out
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
this.treeList1.DataSource = dt;
this.treeList1.KeyFieldName = " Number ";
treeList1.ParentFieldName = " Parent class number ";
treeList1.Columns[0].Caption = " company ";
conn.Close();
// Show check boxes
treeList1.OptionsView.ShowCheckBoxes = true;
}
// When selecting a node , Select all the child nodes of this node When canceling a node , All child nodes of this node are deselected
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);
}
}
// When all the child nodes of a node are selected , Select... For this node When not all the child nodes of a node are selected , This node does not select
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))// As long as any one is different from its selected status, that is, the parent node status is not all selected
{
parentCheckState = CheckState.Unchecked;
break;
}
parentCheckState = check;// otherwise ( The sibling nodes of this node are all selected in the same status ), The selected status of the parent node is the selected status of the node
}
node.ParentNode.CheckState = parentCheckState;
SetCheckedParentNodes(node.ParentNode, check);// Traverse the parent node
}
}
// Trigger the select node event
private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
About this note , In order not to affect everyone's reading experience , I can only show some chapters and core screenshots in the article
afka The producers of
This article has been CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included
copyright:author[mb61ab431c11928],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261250062902.html