Methods for c's treeview binding and getting values

  • 2020-06-19 11:33:19
  • OfStack

Implementation of some features of Treelist

1. The two most basic attributes of data binding: KeyFieldName and ParentFieldName. (These two property 1 Settings are basically hierarchical)

This can be done in code or directly in a property.

This kind of database design is relatively common, 1 general data to satisfy the tree relationship can be designed as such. When binding data, you simply specify DataSource as the corresponding DataTable, KeyFieldName as the table's primary key field, and ParentFieldName as the foreign key field name that the table points to the primary key.


private void BindData()
{
    this.tlOffice.DataSource = dtOffice;
    tlOffice.KeyFieldName = "OfficeID";
    //tlOffice.DataMember = "OfficeName";
    tlOffice.Columns["OfficeName"].Caption = " Bureau of name ";
    tlOffice.ParentFieldName = "ParentOfficeID";
}

2. Realization of basic functions

. When a 1 node is selected, all the children of the node are selected to cancel the 1 node

Which node causes the behavior? Is the node selected or unselected? Two parameters of the method are determined: TreeListNode and CheckState. Traverse the node and its descendants, and set its selected state to the state of the node.


/// <summary>
         ///  Choose a 1 When a node , All children of the node are selected    To cancel a 1 When a node , The children of this node are all unselected 
         /// </summary>
         /// <param name="node"></param>
         /// <param name="state"></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);
             }
         }

. When all the children nodes of a node are selected, the node is selected; A node is not selected unless all of its children are selected


/// <summary>
         ///  When all the children of a node are selected , Node selection     When the children of a node are not all selected , This node is not selected 
         /// </summary>
         /// <param name="node"></param>
         /// <param name="check"></param>
         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 1 Not with its selected status 1 The sample is that the parent node state is not fully selected 
                     {
                         parentCheckState = CheckState.Unchecked;
                         break;
                     }
                     parentCheckState = check;// Otherwise (the node's siblings are all in the same selected state), the parent is the selected state of the node 
                 }

                 node.ParentNode.CheckState = parentCheckState;
                 SetCheckedParentNodes(node.ParentNode, check);// Traversal the parent node 
             }
         }

Don't forget that the above methods are triggered in TreeList_AfterCheckNode:


private void tlOffice_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
    SetCheckedChildNodes(e.Node, e.Node.CheckState);
    SetCheckedParentNodes(e.Node, e.Node.CheckState);
}

3. Get the list of selected check box data


private List<int> lstCheckedOfficeID = new List<int>();// Choose bureau ID A collection of 
            /// <summary>
         ///  Gets the data primary key for the selected state ID A collection of 
         /// </summary>
         /// <param name="parentNode"> The parent node </param>
         private void GetCheckedOfficeID(TreeListNode parentNode)
         {
             if (parentNode.Nodes.Count == 0)
             {
                 return;// The recursion terminates 
             }

             foreach (TreeListNode node in parentNode.Nodes)
             {
                 if (node.CheckState == CheckState.Checked)
                 {
                     DataRowView drv = tlOffice.GetDataRecordByNode(node) as DataRowView;
                                          // The key code, just didn't know how to get the data for a long time ( Who knows can be converted to DataRowView ah )
                     if (drv != null)
                     {
                         int OfficeID = (int)drv["OfficeID"];
                         lstCheckedOfficeID.Add(OfficeID);
                     }

                     
                 }
                 GetCheckedOfficeID(node);
             }
         }

The following test gets the primary key list:


private void btnCheck_Click(object sender, EventArgs e)
         {
             this.lstCheckedOfficeID.Clear();

             if (tlOffice.Nodes.Count > 0)
             {
                 foreach (TreeListNode root in tlOffice.Nodes)
                 {
                     GetCheckedOfficeID(root);
                 }
             }

             string idStr = string.Empty;
             foreach (int id in lstCheckedOfficeID)
             {
                 idStr += id + " ";
             }
             MessageBox.Show(idStr);
         }


Related articles: