DevExpress treelist using the tutorial to bind a multi-level tree

Source: Internet
Author: User

DevExpress treelist using the tutorial to bind a multi-level tree

Overview:The Treelist control can display both the tree structure and other data columns, that is, to establish a parent-child relationship expansion or contraction on one column, and also to display the contents of other columns. Both the node and columns concepts are introduced in Treelist, the first column is the tree structure, the rest is the column of data, and any column can be displayed as a tree column (that is, dragged to the first row)
.

The Treelist control can display both the tree structure and other data columns, that is, to establish a parent-child relationship expansion or contraction on one column, and also to display the contents of other columns.

The node and columns concepts are introduced in Treelist, the first column is the tree structure, the rest is column columns, and any column can be displayed as a tree structure column (that is, dragged to the first row).

Implementing multi-level tree--data source binding

Add the columns to be displayed in Treelist (the first column is the tree structure, the other columns are the data column), and specify FieldName as the database column name

Specifies that the Keyfieldname property is the primary key, and the Parentfieldname property is a tree-like grouping column. Note If the column specified by Parentfieldname is null, the tree may be confusing (only one root node, and the other child nodes for this node)

Bind data with Treelist.datasource = DataTable

(reproduced below) three states of the multi-marquee

A tree control is a control that uses a high frequency. The following two features are often required for property controls

1.TreeList has a checkbox, and the node has three states (all child nodes are selected, all child nodes are not selected, and a subset of the child nodes are selected). This is easily accomplished with the Devxpress treelist control.

Set TreeList.OptionsView.ShowCheckBoxes = TRUE//Whether the checkbox is displayed

Set TreeList.OptionsBehavior.AllowIndeterminateCheckState = true; Sets whether the node has an intermediate state, that is, a subset of the child nodes are selected, and a subset is not selected

After setting these two properties, the Treelist is implemented with a checkbox and there are three states of the node.

2. Select the parent node or child node that affects each other, such as selecting the parent node to select all child nodes. Two events Afterchecknode and Beforechecknode bound to a treelist

The code to implement the function is as follows:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e){SetCheckedChildNodes(e.Node, e.Node.CheckState);SetCheckedParentNodes(e.Node, e.Node.CheckState);}private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e){e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);}/// <summary>/// 设置子节点的状态/// </summary>/// <param name="node"></param>/// <param name="check"></param>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);}}/// <summary>/// 设置父节点的状态/// </summary>/// <param name="node"></param>/// <param name="check"></param>private void SetCheckedParentNodes(TreeListNode node, CheckState check){if (node.ParentNode != null){bool b = false;CheckState state;for (int i = 0; i < node.ParentNode.Nodes.Count; i++){state = (CheckState)node.ParentNode.Nodes[i].CheckState;if (!check.Equals(state)){b = !b;break;}}node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;SetCheckedParentNodes(node.ParentNode, check);}}

The vb.net language code is as follows

123456789101112131415161718192021222324252627282930313233343536373839404142434445 Private Sub trlContact_AfterCheckNode(ByVal sender As System.ObjectByVal As DevExpress.XtraTreeList.NodeEventArgs) Handles trlContact.AfterCheckNodeSetCheckedChildNodes(e.Node, e.Node.CheckState)SetCheckedParentNodes(e.Node, e.Node.CheckState)End Sub‘设置子结点的状态Private Sub SetCheckedChildNodes(ByVal node As TreeListNode, ByVal check As CheckState)For As Integer = 0 To node.Nodes.Count - 1node.Nodes(i).CheckState = checkSetCheckedChildNodes(node.Nodes(i), check)NextEnd Sub‘设置父结点的状态Private Sub SetCheckedParentNodes(ByVal node As TreeListNode, ByVal check As CheckState)If node.ParentNode Is Nothing False ThenDim As Boolean FalseDim state As CheckStateFor As Integer = 0 To node.ParentNode.Nodes.Count - 1state = node.ParentNode.Nodes(i).CheckStateIf check.Equals(state) = False Thenb = Not bExit ForEnd IfNextIf Thennode.ParentNode.CheckState = CheckState.IndeterminateElsenode.ParentNode.CheckState = checkEnd IfSetCheckedParentNodes(node.ParentNode, check)End IfEnd SubPrivate Sub trlContact_BeforeCheckNode(ByVal sender As System.ObjectByVal As DevExpress.XtraTreeList.CheckNodeEventArgs) Handles trlContact.BeforeCheckNodeIf e.PrevState = CheckState.Checked Thene.State = CheckState.UncheckedElsee.State = CheckState.CheckedEnd IfEnd Sub

DevExpress treelist using the tutorial to bind a multi-level tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.