Introduction to Devexpress treelist

  • 2021-11-24 02:40:09
  • OfStack

Node collapse this.treeList1.CollapseAll();

1. Introduction

2. Attribute list

1. OptionsSelection:

EnableAppearanceForcusedCell: Whether the Appearance setting of the selected Cell is available. The default is True;;

EnableAppearanceForcusedRow: Whether the Appearance setting of the selected Node is available. Default to True

InvertSelection: Sets whether the selection style applies only to the selected Cell or to all Cell except the selected Cell. The default is False, which is the latter;

MultiSelect: Whether multiple Node can be selected. The default is False.

2. OptionsView:

AutoCalcPreviewLineCount: Whether to automatically calculate the height of the preview segment. Default to True;

AutoWidth: Whether to allow columns to adjust width automatically; The default is True;;

EnableAppearanceEvenRow: The even Node is generated using the

TreeListAppearanceCollection. Appearance provided by the EvenRow property

Settings provided by TreeListAppearanceCollection. Row

Appearance settings. The default is False, which is the latter;

EnableAppearanceOddRow: The odd Node is generated using the

TreeListAppearanceCollection. Appearance provided by the OddRow property

Settings provided by TreeListAppearanceCollection. Row

Appearance settings. The default is False, that is, the latter;

ShowButtons: Whether to display the Expand and Shrink buttons. Default to True;

ShowCloumns: Whether to display column headers. The default is True;;

ShowFocusedFrame: Whether the focus frame is displayed on the Cell where the focus is obtained. Default to True;

ShowHorzLines: Whether a horizontal line is displayed. Default to True;

ShowIndentAsRowStyle: Whether to generate the indentation of Tree with the Appearance setting of the corresponding Node. Default to False

ShowIndicator: Whether to display the indicator panel for Node. The default is True;;

ShowPreview: Whether to display the preview segment. Default to False;

ShowRoot: Whether to display connectors between the root Node. Default to True;

ShowRowFooterSummary: Whether to display grouping footnotes. Default to False;

ShowSummaryFooter: Whether to display summary footnotes. Default to False;

ShowVertLines: Whether to display vertical lines. Default to True;

3. SelectImageList: When Node is selected, the list of pictures is displayed;

4. StateImageList: A list of pictures indicating the status of Node;

3. Events

STEP 4 Use

1. How to hide the column header of TreeList

Set the ShowColumns property of OptionsView of TreeListr to: False

2. How to expand all nodes ExpandAll () by default

tlvWells.ExpandAll();

As well as TreeNode.Expand   =   false   ;

Or you can control the number of layers expanded reeView1.ExpandLevel=10; Expand 10 layers

  this.treeList.Nodes[0].ExpandAll();// 第1层下的所有接点展开

3. How to highlight each node of TreeList?

The code is as follows:


private void treeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
    {
      TreeList node = sender as TreeList;
      if (e.Node == node.FocusedNode)
      {
        e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
        Rectangle r = new Rectangle(e.EditViewInfo.ContentRect.Left, e.EditViewInfo.ContentRect.Top,
        Convert.ToInt32(e.Graphics.MeasureString(e.CellText,  treeList1.Font).Width + 1), Convert.ToInt32(e.Graphics.MeasureString(e.CellText,treeList1.Font).Height));
        e.Graphics.FillRectangle(SystemBrushes.Highlight, r);
        e.Graphics.DrawString(e.CellText, treeList1.Font, SystemBrushes.HighlightText, r);
        e.Handled = true;
      }
    }

4. Two basic attributes of data binding: KeyFieldName and ParentFieldName.

(These two attributes 1 settings can basically achieve hierarchy) can be written through the code to achieve, can also be directly in the attributes directly achieved. This kind of database design is quite common, and 1 kind of data can be designed in this way if it satisfies the tree relationship. When binding data, you only need to specify DataSource as the corresponding DataTable, KeyFieldName as the table primary key field, and ParentFieldName as the foreign key field name of the table pointing to the primary key.


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

5. When a 1 node is selected, all the child nodes of the node are selected. When a 1 node is cancelled, all the child nodes of the node are cancelled

Which node causes the behavior? Is the node selected or unselected? Thus, two parameters of the method are determined: TreeListNode and CheckState. You can traverse the node and its descendants and set its selected state to the state of the node.


    ///  Select a 1 Node time , All children of this node are selected   Cancel a 1 Node time , Deselect all children of this node 
    /// <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);
      }
  } 

The last two steps are written, and don't forget that the above two 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); 
}

6. When all the child nodes of a node are selected, the node is selected; When not all the children of a node are selected, the node is not selected


 ///  When all the children of a node are selected , The node selects    When not all children of a node are selected , This node does not select 
    /// <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 it is arbitrary 1 Number and its selected status are not 1 Sample means that the parent node status is not fully selected 
          {
            parentCheckState = CheckState.Unchecked;
            break;
          }
          parentCheckState = check;// Otherwise (the siblings of the node have the same selected status), the parent node selected status is the selected status of the node 
        }
        node.ParentNode.CheckState = parentCheckState;
        SetCheckedParentNodes(node.ParentNode, check);// Traversing superior nodes 
      }
    }

The last two steps are written, and don't forget that the above two 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);
    }

7. Get the selected check box data list


 private List<int> lstCheckedOfficeID = new List<int>();// Selection Bureau ID Set 
    ///  Gets the data primary key of the selected state ID Set 
    /// <param name="parentNode"> Parent node </param>
    private void GetCheckedOfficeID(TreeListNode parentNode)
    {
      if (parentNode.Nodes.Count == 0)
      {
        return;// Recursive termination 
      }
      foreach (TreeListNode node in parentNode.Nodes)
      {
        if (node.CheckState == CheckState.Checked)
        {
          DataRowView drv = tlOffice.GetDataRecordByNode(node) as DataRowView;// Key code, just don't know how to get data and struggle for a long time ( Who knows it can be converted to DataRowView Ah )
          if (drv != null)
          {
            int OfficeID = (int)drv["OfficeID"];
            lstCheckedOfficeID.Add(OfficeID);
          } 
        }
        GetCheckedOfficeID(node);
      }
    }

The following test gets the list of primary keys:


 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);
    }

5. Considerations

1. Read data from the database

Method 1: Click the control directly

After this method is connected, the system will automatically generate 1 line of code:


 this.  Database table name TableAdapter.Fill(this.dataDataSet4. Database table name );

This method will not be generated like writing code, and the first line will be defaulted to the root node after connection. And once you want to take away the executable file, it will not be available. Because when you choose the database, you choose the absolute path. So it's best to use the following methods.

Method 2:

Connect to the database with code (there are many ways to write code)


 String connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
      connectionString += @"D:\Data.mdb";// This uses an absolute path 

Relative paths should be used.


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

Well, the above content is this site to give you a brief introduction to Devexpress treelist knowledge, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: