TreeList 的 增加、删除节点-----DevExpress

    技术2025-06-22  25

     注意采用了两个treeListColumn,一个用来展示名称,一个用来展示编码.这样的好处在于作品名维护的时候,可以调用编码,而不需要重新匹配对比了.下面是完整的代码. using System; using System.Drawing; using System. Collections; using System.ComponentModel; using System.Windows.Forms;

    namespace WindowsApplication1 {     /** <summary>     /// typemanage 的摘要说明。     /// </summary>     public class typemanage : System.Windows.Forms.Form      {         private DevExpress.XtraEditors.TextEdit textEdit1;         private DevExpress.XtraTreeList.TreeList treeList1;         private DevExpress.XtraTreeList.Columns.TreeListColumn treeListColumn1;         private DevExpress.XtraEditors.Repository.RepositoryItemTextEdit repositoryItemTextEdit1;         private DevExpress.XtraEditors.SimpleButton simpleButton1;         private DevExpress.XtraEditors.SimpleButton simpleButton2;         private DevExpress.XtraEditors.SimpleButton simpleButton3;         private System.Windows.Forms.Label label1;         int nodeid=0;         DevExpress.XtraTreeList.Nodes.TreeListNode deletenode;         private DevExpress.XtraTreeList.Columns.TreeListColumn treeListColumn2;         private DevExpress.XtraEditors.Repository.RepositoryItemTextEdit repositoryItemTextEdit2;         private System.Windows.Forms.Label label2;         private DevExpress.XtraEditors.TextEdit textEdit2;         /** <summary>         /// 必需的设计器变量。         /// </summary>         private System.ComponentModel.Container components = null;

            public typemanage()          {             //             // Windows 窗体设计器支持所必需的             //             InitializeComponent();

                //             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码             //         }

            /** <summary>         /// 清理所有正在使用的资源。         /// </summary>         protected override void Dispose( bool disposing )          {             if( disposing )              {                 if(components != null)                  {                     components.Dispose();                 }             }             base.Dispose( disposing );         }

            Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码         /** <summary>         /// 设计器支持所需的方法 - 不要使用代码编辑器修改         /// 此方法的内容。         /// </summary>         private void InitializeComponent()          {             this.textEdit1 = new DevExpress.XtraEditors.TextEdit();             this.treeList1 = new DevExpress.XtraTreeList.TreeList();             this.treeListColumn1 = new DevExpress.XtraTreeList.Columns.TreeListColumn();             this.repositoryItemTextEdit1 = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();             this.simpleButton1 = new DevExpress.XtraEditors.SimpleButton();             this.simpleButton2 = new DevExpress.XtraEditors.SimpleButton();             this.simpleButton3 = new DevExpress.XtraEditors.SimpleButton();             this.label1 = new System.Windows.Forms.Label();             this.treeListColumn2 = new DevExpress.XtraTreeList.Columns.TreeListColumn();             this.repositoryItemTextEdit2 = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();             this.label2 = new System.Windows.Forms.Label();             this.textEdit2 = new DevExpress.XtraEditors.TextEdit();             ((System.ComponentModel.ISupportInitialize)(this.textEdit1.Properties)).BeginInit();             ((System.ComponentModel.ISupportInitialize)(this.treeList1)).BeginInit();             ((System.ComponentModel.ISupportInitialize)(this.repositoryItemTextEdit1)).BeginInit();             ((System.ComponentModel.ISupportInitialize)(this.repositoryItemTextEdit2)).BeginInit();             ((System.ComponentModel.ISupportInitialize)(this.textEdit2.Properties)).BeginInit();             this.SuspendLayout();             //              // textEdit1             //              this.textEdit1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));             this.textEdit1.EditValue = "新增1";             this.textEdit1.Location = new System.Drawing.Point(88, 248);             this.textEdit1.Name = "textEdit1";             this.textEdit1.Size = new System.Drawing.Size(128, 21);             this.textEdit1.TabIndex = 12;             //              // treeList1             //              this.treeList1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)                  | System.Windows.Forms.AnchorStyles.Right)));             this.treeList1.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[]  {                                                                                                      this.treeListColumn1,                                                                                                      this.treeListColumn2});             this.treeList1.Location = new System.Drawing.Point(224, 16);             this.treeList1.Name = "treeList1";             this.treeList1.BeginUnboundLoad();             this.treeList1.AppendNode(new object[]  {                                                        "型材",                                                        "01"}, -1);             this.treeList1.AppendNode(new object[]  {                                                        "门窗五金",                                                        "02"}, -1);             this.treeList1.AppendNode(new object[]  {                                                        "辅料",                                                        "03"}, -1);             this.treeList1.AppendNode(new object[]  {                                                        "中空玻璃用料",                                                        "04"}, -1);             this.treeList1.EndUnboundLoad();             this.treeList1.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[]  {                                                                                                                this.repositoryItemTextEdit1,                                                                                                                this.repositoryItemTextEdit2});             this.treeList1.Size = new System.Drawing.Size(264, 264);             this.treeList1.Styles.AddReplace("Preview", new DevExpress.Utils.ViewStyle("Preview", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, true, false, DevExpress.Utils.HorzAlignment.Near, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255))), System.Drawing.Color.FromArgb(((System.Byte)(10)), ((System.Byte)(36)), ((System.Byte)(106)))));             this.treeList1.Styles.AddReplace("FooterPanel", new DevExpress.Utils.ViewStyle("FooterPanel", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Far, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(212)), ((System.Byte)(208)), ((System.Byte)(200))), System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(0)))));             this.treeList1.Styles.AddReplace("GroupButton", new DevExpress.Utils.ViewStyle("GroupButton", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(252)), ((System.Byte)(248)), ((System.Byte)(240))), System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(0)))));             this.treeList1.Styles.AddReplace("EvenRow", new DevExpress.Utils.ViewStyle("EvenRow", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(212)), ((System.Byte)(208)), ((System.Byte)(200))), System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(0)))));             this.treeList1.Styles.AddReplace("TreeLine", new DevExpress.Utils.ViewStyle("TreeLine", "TreeList", new System.Drawing.Font("Microsoft Sans Serif", 8F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255))), System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(128)))));             this.treeList1.Styles.AddReplace("PressedColumn", new DevExpress.Utils.ViewStyle("PressedColumn", "TreeList", new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Bold), "HeaderPanel", ((DevExpress.Utils.StyleOptions)(((DevExpress.Utils.StyleOptions.StyleEnabled | DevExpress.Utils.StyleOptions.UseBackColor)                  | DevExpress.Utils.StyleOptions.UseForeColor))), true, false, false, DevExpress.Utils.HorzAlignment.Near, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(10)), ((System.Byte)(36)), ((System.Byte)(106))), System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)))));             this.treeList1.Styles.AddReplace("HideSelectionRow", new DevExpress.Utils.ViewStyle("HideSelectionRow", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", ((DevExpress.Utils.StyleOptions)((((((DevExpress.Utils.StyleOptions.StyleEnabled | DevExpress.Utils.StyleOptions.UseBackColor)                  | DevExpress.Utils.StyleOptions.UseDrawFocusRect)                  | DevExpress.Utils.StyleOptions.UseFont)                  | DevExpress.Utils.StyleOptions.UseForeColor)                  | DevExpress.Utils.StyleOptions.UseImage))), true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(128))), System.Drawing.Color.FromArgb(((System.Byte)(212)), ((System.Byte)(208)), ((System.Byte)(200)))));             this.treeList1.Styles.AddReplace("GroupFooter", new DevExpress.Utils.ViewStyle("GroupFooter", "TreeList", new System.Drawing.Font("Tahoma", 8F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Far, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(222)), ((System.Byte)(218)), ((System.Byte)(210))), System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(0)))));             this.treeList1.Styles.AddReplace("HorzLine", new DevExpress.Utils.ViewStyle("HorzLine", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(212)), ((System.Byte)(208)), ((System.Byte)(200))), System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(128)))));             this.treeList1.Styles.AddReplace("HeaderPanel", new DevExpress.Utils.ViewStyle("HeaderPanel", "TreeList", new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Bold), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Near, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(212)), ((System.Byte)(208)), ((System.Byte)(200))), System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(0)))));             this.treeList1.Styles.AddReplace("Empty", new DevExpress.Utils.ViewStyle("Empty", "TreeList", new System.Drawing.Font("Microsoft Sans Serif", 8F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(232)), ((System.Byte)(228)), ((System.Byte)(220))), System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)))));             this.treeList1.Styles.AddReplace("VertLine", new DevExpress.Utils.ViewStyle("VertLine", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Near, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(212)), ((System.Byte)(208)), ((System.Byte)(200))), System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(128)))));             this.treeList1.Styles.AddReplace("FocusedCell", new DevExpress.Utils.ViewStyle("FocusedCell", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", ((DevExpress.Utils.StyleOptions)((((((DevExpress.Utils.StyleOptions.StyleEnabled | DevExpress.Utils.StyleOptions.UseBackColor)                  | DevExpress.Utils.StyleOptions.UseDrawFocusRect)                  | DevExpress.Utils.StyleOptions.UseFont)                  | DevExpress.Utils.StyleOptions.UseForeColor)                  | DevExpress.Utils.StyleOptions.UseImage))), true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(225))), System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(0)))));             this.treeList1.Styles.AddReplace("OddRow", new DevExpress.Utils.ViewStyle("OddRow", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255))), System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(0)))));             this.treeList1.Styles.AddReplace("SelectedRow", new DevExpress.Utils.ViewStyle("SelectedRow", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", ((DevExpress.Utils.StyleOptions)((((((DevExpress.Utils.StyleOptions.StyleEnabled | DevExpress.Utils.StyleOptions.UseBackColor)                  | DevExpress.Utils.StyleOptions.UseDrawFocusRect)                  | DevExpress.Utils.StyleOptions.UseFont)                  | DevExpress.Utils.StyleOptions.UseForeColor)                  | DevExpress.Utils.StyleOptions.UseImage))), true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(20)), ((System.Byte)(46)), ((System.Byte)(116))), System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)))));             this.treeList1.Styles.AddReplace("FocusedRow", new DevExpress.Utils.ViewStyle("FocusedRow", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", ((DevExpress.Utils.StyleOptions)((((((DevExpress.Utils.StyleOptions.StyleEnabled | DevExpress.Utils.StyleOptions.UseBackColor)                  | DevExpress.Utils.StyleOptions.UseDrawFocusRect)                  | DevExpress.Utils.StyleOptions.UseFont)                  | DevExpress.Utils.StyleOptions.UseForeColor)                  | DevExpress.Utils.StyleOptions.UseImage))), true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(10)), ((System.Byte)(36)), ((System.Byte)(106))), System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)))));             this.treeList1.Styles.AddReplace("Row", new DevExpress.Utils.ViewStyle("Row", "TreeList", new System.Drawing.Font("Tahoma", 10F), "", DevExpress.Utils.StyleOptions.StyleEnabled, true, false, false, DevExpress.Utils.HorzAlignment.Default, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255))), System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(0)))));             this.treeList1.TabIndex = 23;             this.treeList1.Text = "类型维护";             this.treeList1.FocusedNodeChanged += new DevExpress.XtraTreeList.FocusedNodeChangedEventHandler(this.treeList1_FocusedNodeChanged);             this.treeList1.AfterFocusNode += new DevExpress.XtraTreeList.NodeEventHandler(this.treeList1_AfterFocusNode);             //              // treeListColumn1             //              this.treeListColumn1.Caption = "类型维护----绿城铝业";             this.treeListColumn1.ColumnEdit = this.repositoryItemTextEdit1;             this.treeListColumn1.FieldName = "类型维护----绿城铝业";             this.treeListColumn1.Name = "treeListColumn1";             this.treeListColumn1.VisibleIndex = 0;             //              // repositoryItemTextEdit1             //              this.repositoryItemTextEdit1.AutoHeight = false;             this.repositoryItemTextEdit1.Name = "repositoryItemTextEdit1";             //              // simpleButton1             //              this.simpleButton1.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.Office2003;             this.simpleButton1.Location = new System.Drawing.Point(40, 24);             this.simpleButton1.Name = "simpleButton1";             this.simpleButton1.TabIndex = 27;             this.simpleButton1.Text = " 新增根";             this.simpleButton1.Click += new System.EventHandler(this.simpleButton1_Click);             //              // simpleButton2             //              this.simpleButton2.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.Office2003;             this.simpleButton2.Location = new System.Drawing.Point(40, 128);             this.simpleButton2.Name = "simpleButton2";             this.simpleButton2.TabIndex = 28;             this.simpleButton2.Text = "删除节点";             this.simpleButton2.Click += new System.EventHandler(this.simpleButton2_Click);             //              // simpleButton3             //              this.simpleButton3.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.Office2003;             this.simpleButton3.Location = new System.Drawing.Point(40, 80);             this.simpleButton3.Name = "simpleButton3";             this.simpleButton3.TabIndex = 30;             this.simpleButton3.Text = " 新增子极";             this.simpleButton3.Click += new System.EventHandler(this.simpleButton3_Click);             //              // label1             //              this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));             this.label1.Location = new System.Drawing.Point(8, 256);             this.label1.Name = "label1";             this.label1.Size = new System.Drawing.Size(80, 16);             this.label1.TabIndex = 31;             this.label1.Text = "新增节点名称";             //              // treeListColumn2             //              this.treeListColumn2.Caption = "对应编码";             this.treeListColumn2.ColumnEdit = this.repositoryItemTextEdit2;             this.treeListColumn2.FieldName = "treeListColumn2";             this.treeListColumn2.Name = "treeListColumn2";             this.treeListColumn2.VisibleIndex = 1;             //              // repositoryItemTextEdit2             //              this.repositoryItemTextEdit2.AutoHeight = false;             this.repositoryItemTextEdit2.Name = "repositoryItemTextEdit2";             //              // label2             //              this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));             this.label2.Location = new System.Drawing.Point(8, 288);             this.label2.Name = "label2";             this.label2.Size = new System.Drawing.Size(80, 16);             this.label2.TabIndex = 33;             this.label2.Text = "节点对应代码";             //              // textEdit2             //              this.textEdit2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));             this.textEdit2.EditValue = "05";             this.textEdit2.Location = new System.Drawing.Point(88, 280);             this.textEdit2.Name = "textEdit2";             this.textEdit2.Size = new System.Drawing.Size(128, 21);             this.textEdit2.TabIndex = 32;             //              // typemanage             //              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);             this.ClientSize = new System.Drawing.Size(496, 333);             this.Controls.Add(this.label2);             this.Controls.Add(this.textEdit2);             this.Controls.Add(this.label1);             this.Controls.Add(this.simpleButton3);             this.Controls.Add(this.simpleButton2);             this.Controls.Add(this.simpleButton1);             this.Controls.Add(this.treeList1);             this.Controls.Add(this.textEdit1);             this.Name = "typemanage";             this.Text = "类型管理";             ((System.ComponentModel.ISupportInitialize)(this.textEdit1.Properties)).EndInit();             ((System.ComponentModel.ISupportInitialize)(this.treeList1)).EndInit();             ((System.ComponentModel.ISupportInitialize)(this.repositoryItemTextEdit1)).EndInit();             ((System.ComponentModel.ISupportInitialize)(this.repositoryItemTextEdit2)).EndInit();             ((System.ComponentModel.ISupportInitialize)(this.textEdit2.Properties)).EndInit();             this.ResumeLayout(false);

            }         #endregion

            private void simpleButton1_Click(object sender, System.EventArgs e)          {             if(textEdit1.Text.Trim()==""||textEdit2.Text.Trim()=="")              {                 MessageBox.Show("请输入对应名称及编码!","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);                 return;             }             this.treeList1.AppendNode(new object[]  {this.textEdit1.Text,textEdit2.Text},-1);

            //            newnode.         //            this.treeList1.Nodes[0].Nodes.Add(newnode);        }

            private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)          {             MessageBox.Show(e.Node.Level.ToString());             MessageBox.Show(e.Node.Id.ToString());             MessageBox.Show(e.Node.GetDisplayText(0));         }

            private void treeList1_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)          {             nodeid=e.Node.Id;             deletenode=e.Node;         }

            private void simpleButton3_Click(object sender, System.EventArgs e)          {             if(textEdit1.Text.Trim()==""||textEdit2.Text.Trim()=="")              {                 MessageBox.Show("请输入对应名称及编码!","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);                 return;             }             this.treeList1.AppendNode(new object[]  {this.textEdit1.Text,textEdit2.Text},nodeid);         }

            private void simpleButton2_Click(object sender, System.EventArgs e)          { //            MessageBox.Show(nodeid.ToString());             this.treeList1.DeleteNode(deletenode);         }     } }  

    最新回复(0)