How to set the data source to display as a single column tree structure

Keywords: C# Programming Attribute

scene

Winform control - DevExpress18 download installation registration and use in VS:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243

After the above installation of DevExpress has been implemented, drag a TreeList, and then how to set the data source for it.

How to set the data source in TreeList of DevExpress? Start with the instance:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102548490

Refer to the above procedure for setting data source.

How to set parameters and data sources if you want to implement such a single column tree structure.

 

 

Note:

Blog home page:
https://blog.csdn.net/badao_liumang_qizhi
Follow public account
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

First create a Winform program, then drag and drop a TreeList control

Then double click to enter the loading event of the form. If the data source is set elsewhere, the same is true.

First, set TreeList style, etc.

 

#region Set column head, node indicator panel, table line style

            treeList1.OptionsView.ShowColumns = false;             //Hide column headers
            treeList1.OptionsView.ShowIndicator = false;           //Hide node indicator panel
            treeList1.OptionsView.ShowHorzLines = false;           //Hide horizontal table lines
            treeList1.OptionsView.ShowVertLines = false;           //Hide vertical table lines
            treeList1.OptionsView.ShowIndentAsRowStyle = false;

            #endregion

            #region Initially disable cell selection and entire row selection

            treeList1.OptionsView.ShowFocusedFrame = true;                               //Set display focus box
            treeList1.OptionsSelection.EnableAppearanceFocusedCell = false;              //Disable cell selection
            treeList1.OptionsSelection.EnableAppearanceFocusedRow = false;               //Disable positive row selection

            #endregion

            #region Set up TreeList Expand collapse button style and tree line style of

            treeList1.OptionsView.ShowButtons = true;                  //Show expand collapse button
            treeList1.LookAndFeel.UseDefaultLookAndFeel = false;       //Disable default appearance and feel
            treeList1.LookAndFeel.UseWindowsXPTheme = true;            //Use WindowsXP theme
            treeList1.TreeLineStyle = DevExpress.XtraTreeList.LineStyle.Percent50;     //Style tree lines

            #endregion

 

Then set its single column display

#region Add single row

            DevExpress.XtraTreeList.Columns.TreeListColumn colNode = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            colNode.Name = String.Format("col{0}", "NodeText");
            colNode.Caption = "NodeText";
            colNode.FieldName = "NodeText";
            colNode.VisibleIndex = 0;
            colNode.Visible = true;

            colNode.OptionsColumn.AllowEdit = false;                        //Allow editing
            colNode.OptionsColumn.AllowMove = false;                        //Move allowed or not
            colNode.OptionsColumn.AllowMoveToCustomizationForm = false;     //Allow to move to custom form
            colNode.OptionsColumn.AllowSort = false;                        //Allow sorting
            colNode.OptionsColumn.FixedWidth = false;                       //Fixed column width or not
            colNode.OptionsColumn.ReadOnly = true;                          //Is it read-only?
            colNode.OptionsColumn.ShowInCustomizationForm = true;           //Allow display in custom form after removing columns

            treeList1.Columns.Clear();
            treeList1.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { colNode });
            #endregion

 

Note:

The FieldName here corresponds to the property name to be displayed in the data source.

Then set the primary key field as the identification node, where Id is used to distinguish each node; set the tag attribute of its parent node.

treeList1.KeyFieldName = "Id";
treeList1.ParentFieldName = "ParentId";

 

In order to build the data source, we need to create a new DataTreeNode class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TreeListTest
{
    public class DataTreeNode
    {
        private string id;
        private string parentId;
        private string nodeText;


        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        public string ParentId
        {
            get { return parentId; }
            set { parentId = value; }
        }

        public string NodeText
        {
            get { return nodeText; }
            set { nodeText = value; }
        }


    }

}

 

Then go back and build the data source.

   DataTreeNode node1 = new DataTreeNode();
            node1.Id = "1";
            node1.ParentId = null;
            node1.NodeText = "1 Public address";

            DataTreeNode node11 = new DataTreeNode();
            node11.Id = "2";
            node11.ParentId = "1";
            node11.NodeText = "1-1 Domineering procedural ape";

            DataTreeNode node111 = new DataTreeNode();
            node111.Id = "3";
            node111.ParentId = "2";
            node111.NodeText = "1-1-1 Large programming resources";


            List<DataTreeNode> data = new List<DataTreeNode>();

            data.Add(node1);
            data.Add(node11);
            data.Add(node111);

 

Then set the data source for TreeList

treeList1.DataSource = data;
treeList1.RefreshDataSource();

 

Final complete form loading method code

 

private void Form1_Load(object sender, EventArgs e)
        {
            #region Set column head, node indicator panel, table line style

            treeList1.OptionsView.ShowColumns = false;             //Hide column headers
            treeList1.OptionsView.ShowIndicator = false;           //Hide node indicator panel
            treeList1.OptionsView.ShowHorzLines = false;           //Hide horizontal table lines
            treeList1.OptionsView.ShowVertLines = false;           //Hide vertical table lines
            treeList1.OptionsView.ShowIndentAsRowStyle = false;

            #endregion

            #region Initially disable cell selection and entire row selection

            treeList1.OptionsView.ShowFocusedFrame = true;                               //Set display focus box
            treeList1.OptionsSelection.EnableAppearanceFocusedCell = false;              //Disable cell selection
            treeList1.OptionsSelection.EnableAppearanceFocusedRow = false;               //Disable positive row selection

            #endregion

            #region Set up TreeList Expand collapse button style and tree line style of

            treeList1.OptionsView.ShowButtons = true;                  //Show expand collapse button
            treeList1.LookAndFeel.UseDefaultLookAndFeel = false;       //Disable default appearance and feel
            treeList1.LookAndFeel.UseWindowsXPTheme = true;            //Use WindowsXP theme
            treeList1.TreeLineStyle = DevExpress.XtraTreeList.LineStyle.Percent50;     //Style tree lines

            #endregion

            #region Add single row

            DevExpress.XtraTreeList.Columns.TreeListColumn colNode = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            colNode.Name = String.Format("col{0}", "NodeText");
            colNode.Caption = "NodeText";
            colNode.FieldName = "NodeText";
            colNode.VisibleIndex = 0;
            colNode.Visible = true;

            colNode.OptionsColumn.AllowEdit = false;                        //Allow editing
            colNode.OptionsColumn.AllowMove = false;                        //Move allowed or not
            colNode.OptionsColumn.AllowMoveToCustomizationForm = false;     //Allow to move to custom form
            colNode.OptionsColumn.AllowSort = false;                        //Allow sorting
            colNode.OptionsColumn.FixedWidth = false;                       //Fixed column width or not
            colNode.OptionsColumn.ReadOnly = true;                          //Is it read-only?
            colNode.OptionsColumn.ShowInCustomizationForm = true;           //Allow display in custom form after removing columns

            treeList1.Columns.Clear();
            treeList1.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { colNode });

            #endregion

            treeList1.KeyFieldName = "Id";
            treeList1.ParentFieldName = "ParentId";

            DataTreeNode node1 = new DataTreeNode();
            node1.Id = "1";
            node1.ParentId = null;
            node1.NodeText = "1 Public address";

            DataTreeNode node11 = new DataTreeNode();
            node11.Id = "2";
            node11.ParentId = "1";
            node11.NodeText = "1-1 Domineering procedural ape";

            DataTreeNode node111 = new DataTreeNode();
            node111.Id = "3";
            node111.ParentId = "2";
            node111.NodeText = "1-1-1 Large programming resources";


            List<DataTreeNode> data = new List<DataTreeNode>();

            data.Add(node1);
            data.Add(node11);
            data.Add(node111);
            
            treeList1.DataSource = data;
            treeList1.RefreshDataSource();
        }

Posted by carleihar on Fri, 25 Oct 2019 00:49:16 -0700