python treeview写入文件_将TreeView存储、写入数据库的扩展类TreeViewEx

这篇博客介绍了一个C#类`TreeViewToDB`,该类实现了将Python TreeView的内容读取、修改并保存到数据库的功能。通过递归方法处理节点删除,确保数据库与界面同步,同时提供了判断是否允许节点拖放的逻辑,防止循环引用。
摘要由CSDN通过智能技术生成

在一个项目中用到了读取、生成并保存TreeView内容到数据库的应用,经过研究,并着重参考了planet-source-code上的一篇文章,将原文的VB.NET代码改造成了C#代码,与大家共同分享。

using System;

using System.Windows.Forms;

using System.Collections;

using System.Data.OleDb;

namespace CallingCenterAdmin

{

///

/// TreeViewToDB 的摘要说明。

///

public class TreeViewToDB

{

// m_alDeletedNodes is used to store node deletions in memory.  A call to

// CTreeView.SaveNodeCollection() will commit the deletions, and other changes,

// to the database.

public ArrayList m_alDeletedNodes;

public OleDbConnection m_conn;

public TreeViewToDB(OleDbConnection conn)

{

// Initialize the DeletedNodes collection.

m_alDeletedNodes = new ArrayList();

m_conn = conn;

}

public bool DeleteNode(TreeNode tnStart)

{

// PURPOSE: This function will delete the designated node (tnStart) and all

// of its children.  The deletions will be stored in a collection.  This will

// keep the deletions in memory, which configuration will allow us to rollback

// deletions.

// Get a reference to the start node parent.

TreeNode tnParent = tnStart.Parent;

// Delete the start node's children.  This is performed via

// recursion, which will walk through all children regardless of number or

// arrangement.  Walking through each and every child of the start node will

// allow us to synchronize node deletions with the database.  Simply calling

// the remove function will remove the node and its children, but

// will leave orphan records in the database.

if (!DeleteNodeRecursive(tnStart))

{

return false;

}

// Record the deletion of the start node.

m_alDeletedNodes.Add(tnStart);

// Remove the start node from the TreeNodeCollection.

tnStart.Nodes.Remove(tnStart);

return true;

}

public bool DeleteNodeRecursive(TreeNode tnParent)

{

// PURPOSE: This function will walk through all the child nodes for a given

// node.  It will remove all the nodes from the TreeNodeCollection and will

// record all deletions in memory.  Deletions will be committed to the

// database when the user calls the CTreeView.SaveNodeCollection() method.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值