SoFunction
Updated on 2025-03-03

Introduction to Devexpress treelist

Node foldingthis.();

1. Introduction

2. Attribute list

1、OptionsSelection:

EnableAppearanceForcusedCell: Whether the Appearance settings of the selected Cell are available. Default is True;

EnableAppearanceForcusedRow: Whether the Appearance settings of the selected Node are available. Default is True

InvertSelection: Sets whether the selected style is applied only to the selected cell, or to all the cells except the selected cell. Default is False, that is, the latter;

MultiSelect: Is it possible to select multiple Nodes? Default is False.

2、OptionsView:

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

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

EnableAppearanceEvenRow: When generating an even Node, it is adopted

Appearance provided by attributes

Settings or using the provided

Appearance settings. Default is False, i.e. the latter;

EnableAppearanceOddRow: When generating an odd number, it is adopted

Appearance provided by attributes

Settings or using the provided

Appearance settings. Default is False, i.e. the latter;

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

ShowCloums: Whether to display the column title. Default is True;

ShowFocusedFrame: Whether to display the focus frame on the cell that gets focus. Default is True;

ShowHorzLines: Whether to display horizontal lines. Default is True;

ShowIndentAsRowStyle: Whether to use the Appearance settings of the corresponding Node to generate the indentation of the Tree. Default is False

ShowIndicator: Whether to display the indicator panel of Node. Default is True;

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

ShowRoot: Whether to display connection lines between root Nodes. Default is True;

ShowRowFooterSummary: Whether to display group footnotes. Default is False;

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

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

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

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

III. Events

4. Use

1. How to hide the column header of TreeList

Set the ShowColumns property of TreeListr's OptionsView to: False

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

   ();

as well as   =   false  

Or you can control the number of expanded layers =10; expand 10 layers

[0].ExpandAll();// All contacts under the first layer are expanded

3. How to highlight each node of TreeList?

The code is as follows:

private void treeList1_CustomDrawNodeCell(object sender,  e)
    {
      TreeList node = sender as TreeList;
      if ( == )
      {
        (, );
        Rectangle r = new Rectangle(, ,
        Convert.ToInt32((,  ).Width + 1), Convert.ToInt32((,).Height));
        (, r);
        (, , , r);
         = true;
      }
    }

4. The two most basic properties of data binding: KeyFieldName and ParentFieldName.

(These two properties can basically be graded as soon as they are set) It can be implemented through code writing, or it can be directly implemented in the properties. This kind of database design is relatively common. Generally, the data can be designed in this way if it satisfies a tree relationship. When binding data, you only need to specify DataSource as the corresponding DataTable, specify KeyFieldName as the table primary key field, and ParentFieldName as the foreign key field name that the table points to the primary key.

 private void BindData()
      {
       = dtOffice;
       = "OfficeID";
      // = "OfficeName";
      ["OfficeName"].Caption = "Office Name";
       = "ParentOfficeID";
    }

5. When selecting a certain node, all children of the node are selected. When canceling a certain node, all children of the node are cancelled.

Which node causes behavior? Is the node selected or unchecked? This determines two parameters of the method: TreeListNode and CheckState. Just traverse the node and its descendants and set its selected state to the state of the node.

    /// When selecting a certain node, all children of the node are selected. When canceling a certain node, all children of the node are cancelled    /// <param name="node"></param>
    /// <param name="state"></param>
    private void SetCheckedChildNodes(TreeListNode node, CheckState check)
    {
      for (int i = 0; i < ; i++)
      {
        [i].CheckState = check;
        SetCheckedChildNodes([i], check);
      }
  } 

The previous two steps have been written, don't forget that the above two methods are triggered in TreeList_AfterCheckNode:

private void tlOffice_AfterCheckNode(object sender,  e) 
{ 
  SetCheckedChildNodes(, ); 
  SetCheckedParentNodes(, ); 
}

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

 /// When all children of a node are selected, the node selects. When all children of a node are not selected, the node does not select.    /// <param name="node"></param>
    /// <param name="check"></param>
    private void SetCheckedParentNodes(TreeListNode node, CheckState check)
    {
      if ( != null)
      {
        CheckState parentCheckState = ;
        CheckState nodeCheckState;
        for (int i = 0; i < ; i++)
        {
          nodeCheckState = (CheckState)[i].CheckState;
          if (!(nodeCheckState))//As long as any one is different from its selected state, the parent node status is not selected in all cases          {
            parentCheckState = ;
            break;
          }
          parentCheckState = check;// Otherwise (the selected status of the brother node of the node is the same), the selected status of the parent node is the selected status of the node        }
         = parentCheckState;
        SetCheckedParentNodes(, check);//Travel over the previous node      }
    }

The previous two steps have been written, don't forget that the above two methods are triggered in TreeList_AfterCheckNode:

private void tlOffice_AfterCheckNode(object sender,  e)
    {
      SetCheckedChildNodes(, );
      SetCheckedParentNodes(, );
    }

7. Get the selected checkbox data list

 private List&lt;int&gt; lstCheckedOfficeID = new List&lt;int&gt;();//Select the Bureau ID collection    /// Get the data primary key ID set of selected state    /// <param name="parentNode">parent node</param>    private void GetCheckedOfficeID(TreeListNode parentNode)
    {
      if ( == 0)
      {
        return;//Recursive termination      }
      foreach (TreeListNode node in )
      {
        if ( == )
        {
          DataRowView drv = (node) as DataRowView;//The key code is just because I don’t know how to get data, and I have been struggling for a long time (I know that it can be converted to DataRowView)          if (drv != null)
          {
            int OfficeID = (int)drv["OfficeID"];
            (OfficeID);
          } 
        }
        GetCheckedOfficeID(node);
      }
    }

The following test gets the primary key list:

 private void btnCheck_Click(object sender, EventArgs e)
    {
      ();
      if ( > 0)
      {
        foreach (TreeListNode root in )
        {
          GetCheckedOfficeID(root);
        }
      }
      string idStr = ;
      foreach (int id in lstCheckedOfficeID)
      {
        idStr += id + " ";
      }
      (idStr);
    }

5. Things to note

1. Read data from the database

Method 1: Click the control directly

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

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

After this method is generated, it will not be connected like writing code, and then the first line will be defaulted to the root node. And once you want to take the executable, it is unavailable. Because when you choose the database, you choose the absolute path. So it is best to use the following method.

Method 2:

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

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

Relative paths should be used.

(String connectionString = @"Provider=.4.0;Data Source=";
      connectionString +=  + @"\";
 )
       con = new (connectionString);
      ();
       cmd = new ("SELECT * FROM FS_STAFF", con);
       reader = ();
      DataTable table = new DataTable();
      (reader);
       = table;

Okay, the above content is the Devexpress treelist knowledge briefly introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!