- private void FrmDictionaryManaged_Load(object sender, EventArgs e)
- {
- // treeList1.DataSource= CreateDataTable();
-
- treeList2.DataSource = CreateTreeList();
- // 绑定TreeList控件到数据源
- treeList1.DataSource = GetData();
- treeList1.KeyFieldName = "ID";
- treeList1.ParentFieldName = "ParentID";
-
- this.treeList1.Columns[1].Caption = @"字典测试";//树的名称
-
- // 设置列为只读
- treeList1.Columns.Cast
().ToList().ForEach(column => - {
- column.OptionsColumn.ReadOnly = true;
- column.OptionsColumn.AllowEdit = false;
- });
- treeList1.Columns["Id"].Visible = false;
- }
- private BindingList
GetData() - {
- // 此属性设为true 每个节点前会自动增加一个check框
- // treeList1.OptionsView.ShowCheckBoxes = true;
-
- // 此属性设为true 点击复选框时,可设置为半选状态 ??效果还未呈现出来
- // treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;
-
-
- // 这里返回一个包含节点数据的BindingList
- // 你可以根据实际情况替换为你自己的数据源
- BindingList
data = new BindingList - {
- new TreeNode { Id = 1, Name = "性别" },
-
- // 其他节点...
- };
-
- return data;
- }
- public class TreeNode
- {
- public int Id { get; set; }
- public string Name { get; set; }
- }
-
- public DataTable CreateDataTable()
- {
- DataTable dt = new DataTable();
-
- //此属性设为true 每个节点前会自动增加一个check框
- treeList1.OptionsView.ShowCheckBoxes = true;
-
- //此属性设为true 点击复选框时,可设置为半选状态 ??效果还未呈现出来
- treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;
-
- dt.Columns.Add("ID");
- dt.Columns.Add("PARENTID");
- dt.Columns.Add("NAME");
-
-
- //为此表增加数据
- DataRow dr1 = dt.NewRow();
- dr1["ID"] = 1;
- dr1["PARENTID"] = 1;
- dr1["NAME"] = "类目1";
- dt.Rows.Add(dr1);
-
- DataRow dr2 = dt.NewRow();
- dr2["ID"] = 2;
- dr2["PARENTID"] = 1;
- dr2["NAME"] = "商品1";
- dt.Rows.Add(dr2);
-
- DataRow dr3 = dt.NewRow();
- dr3["ID"] = 3;
- dr3["PARENTID"] = 1;
- dr3["NAME"] = "商品2";
- dt.Rows.Add(dr3);
- DataRow dr31 = dt.NewRow();
- dr31["ID"] = 7;
- dr31["PARENTID"] = 1;
- dr31["NAME"] = "类目3";
- dt.Rows.Add(dr31);
-
- DataRow dr4 = dt.NewRow();
- dr4["ID"] = 4;
- dr4["PARENTID"] = 4;
- dr4["NAME"] = "类目2";
- dt.Rows.Add(dr4);
-
- DataRow dr5 = dt.NewRow();
- dr5["ID"] = 5;
- dr5["PARENTID"] = 4;
- dr5["NAME"] = "商品1";
- dt.Rows.Add(dr5);
-
-
- DataRow dr6 = dt.NewRow();
- dr6["ID"] = 6;
- dr6["PARENTID"] = 4;
- dr6["NAME"] = "商品2";
- dt.Rows.Add(dr6);
-
-
-
- this.treeList1.DataSource = dt;
- this.treeList1.KeyFieldName = "ID";
- this.treeList1.ParentFieldName = "PARENTID";
- this.treeList1.Columns[0].Caption = "测试";//树的名称
-
- return dt;
- }
-
- public DataTable CreateTreeList()
- {
- DataTable dt = new DataTable();
-
- #region TreeList属性设置
-
- //节点前显示复选框
- this.treeList2.OptionsView.ShowCheckBoxes = true;
-
- //此属性设为true 点击复选框时,可设置为半选状态
- treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;
-
-
- #endregion
-
- #region 在TreeList中增加列
- //用于标识当前节点和当前节点的父节点
- dt.Columns.Add("ID");
- dt.Columns.Add("PARENTID");
-
- //第一列数据的名称
- dt.Columns.Add("NAME");
-
- //第二列(节点编码的名称)
- dt.Columns.Add("CODE");
-
- //第三列(其他)
- dt.Columns.Add("OTHERS");
- #endregion
-
- #region 添加数据
-
- //第一行数据
- DataRow dr1 = dt.NewRow();
- dr1["ID"] = 1;
- dr1["PARENTID"] = DBNull.Value;
- dr1["NAME"] = "根节点1";
- dr1["CODE"] = "根节点1编码";
- dr1["OTHERS"] = "根节点1的其他信息";
- dt.Rows.Add(dr1);
-
- //第二行数据
- DataRow dr2 = dt.NewRow();
- dr2["ID"] = 2;
- dr2["PARENTID"] = 1;
- dr2["NAME"] = "当前节点2";
- dr2["CODE"] = "当前节点2编码";
- dr2["OTHERS"] = "当前节点2的其他信息";
- dt.Rows.Add(dr2);
-
- //第三行数据
- DataRow dr3 = dt.NewRow();
- dr3["ID"] = 3;
- dr3["PARENTID"] = 1;
- dr3["NAME"] = "当前节点3";
- dr3["CODE"] = "当前节点3编码";
- dr3["OTHERS"] = "当前节点3的其他信息";
- dt.Rows.Add(dr3);
-
- //第四行数据
- DataRow dr4 = dt.NewRow();
- dr4["ID"] = 4;
- dr4["PARENTID"] = DBNull.Value;
- dr4["NAME"] = "根节点2";
- dr4["CODE"] = "根节点2编码";
- dr4["OTHERS"] = "根节点2的其他信息";
- dt.Rows.Add(dr4);
-
- //第五行数据
- DataRow dr5 = dt.NewRow();
- dr5["ID"] = 5;
- dr5["PARENTID"] = 4;
- dr5["NAME"] = "当前节点5";
- dr5["CODE"] = "当前节点5编码";
- dr5["OTHERS"] = "当前节点5的其他信息";
- dt.Rows.Add(dr5);
-
- //第六行数据
- DataRow dr6 = dt.NewRow();
- dr6["ID"] = 6;
- dr6["PARENTID"] = 4;
- dr6["NAME"] = "当前节点6";
- dr6["CODE"] = "当前节点6编码";
- dr6["OTHERS"] = "当前节点6的其他信息";
- dt.Rows.Add(dr6);
- #endregion
-
- #region 绑定数据源并设置列名
- this.treeList2.DataSource = dt;
- this.treeList2.KeyFieldName = "ID";
- this.treeList2.ParentFieldName = "PARENTID";
-
- //这句一定要写在上面两句的下面
- this.treeList2.Columns[0].Caption = "节点名称";
- this.treeList2.Columns[1].Caption = "节点编码";
- this.treeList2.Columns[2].Caption = "节点其他信息";
- #endregion
-
- return dt;
- }
-
- private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- FrmDictionaryTreeEdit frm = new FrmDictionaryTreeEdit();
- frm.ShowDialog();
-
- if (frm.DialogResult != DialogResult.OK)
- return;
-
- // 创建新节点
- var newNode = new TreeNode { Id = GetNewNodeId(), Name = frm.TypeName };
-
- // 获取选中的节点
- TreeListNode selectedNode = treeList1.FocusedNode;
-
- //if (selectedNode == null)
- //{
- // 如果没有选中节点,添加到根节点
- treeList1.AppendNode(new object[] { newNode.Id, newNode.Name }, null);
- //}
- //else
- //{
- // // 如果有选中节点,添加为选中节点的子节点
- // treeList1.AppendNode(new object[] { newNode.Id, newNode.Name }, selectedNode);
- // selectedNode.Expanded = true; // 展开选中节点以显示子节点
- //}
- }
-
- private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- TreeListNode selectedNode = treeList1.FocusedNode;
-
- if (selectedNode != null)
- {
- FrmDictionaryTreeEdit frm = new FrmDictionaryTreeEdit();
-
- // 获取选中节点的数据
- int nodeId = Convert.ToInt32(selectedNode.GetValue("ID"));
- frm.TypeName = selectedNode.GetValue("Name").ToString();
-
- frm.ShowDialog();
-
- if (frm.DialogResult != DialogResult.OK)
- return;
-
- string editedName = frm.TypeName;
-
- // 更新TreeList中的节点数据
- selectedNode.SetValue("Name", editedName);
- }
- }
-
- private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- TreeListNode selectedNode = treeList1.FocusedNode;
-
- if (selectedNode != null)
- {
- // 删除选中的节点
- treeList1.DeleteNode(selectedNode);
- }
- }
- private int GetNewNodeId()
- {
- // 这里可以根据需要生成新节点的ID,确保其唯一性
- // 例如,你可以查询已存在的节点ID的最大值,然后加1
- int maxId = (from TreeListNode node in treeList1.Nodes select Convert.ToInt32(node.GetValue("ID"))).Concat(new[] { -1 }).Max();
-
- return maxId + 1;
- }
控件:TreeList ,GirdControl,ContextMenuStrip