treeview递归删除

jerysab 2011-05-18 03:49:21
数据库中的表 id pid name
1 0 A
2 1 A1
3 1 A2
4 2 A11

我用的三层。 想在删除父节点时,把他下边的所以节点都删除掉。

代码该如何写呢?
...全文
117 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qgqch2008 2011-05-20
  • 打赏
  • 举报
回复
这个鸟东西我也是搞得一头雾水,关注一下!
htl258_Tony 2011-05-20
  • 打赏
  • 举报
回复
参考:BOM节点删除
Daqing 2011-05-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tsapi 的回复:]
C# code
/// <summary>
/// 删除节点
/// </summary>
/// <param name="node"></param>
private int idelcount = 0;
private void DeleteNodeToDB(TreeNode node)
……
[/Quote]

SqlCommand com=new Sqlcommand(con,"delete from A where ID='"+name+"'");
这句参数写反了,你换一下。
Daqing 2011-05-19
  • 打赏
  • 举报
回复
   /// <summary>
/// 删除节点
/// </summary>
/// <param name="node"></param>
private int idelcount = 0;
private void DeleteNodeToDB(TreeNode node)
{

TreeNode currentnode = new TreeNode();
currentnode=node;
if (currentnode != null)
{
string name=currentnode.Name;
SqlConnection con=new SqlConnection(strconn);//你的sql连接语句,这你我不写了
SqlCommand com=new Sqlcommand(con,"delete from A where ID='"+name+"'");
if(con.State == ConnectionState.Close)
con.Open();
idelcount +=com.ExecuteNonQuery()
con.Close();//由于是递归,这里每次都要打开关闭一次。
}
if (currentnode.Nodes.Count > 0)
{
for (int j = 0; j < currentnode.Nodes.Count; j++)
{
DeleteNodeToDB(currentnode.Nodes[j]);
}
}

}
xingheng907 2011-05-18
  • 打赏
  • 举报
回复
牛人!观望中。。。。
jerysab 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tsapi 的回复:]
C# code
/// <summary>
/// 删除节点
/// </summary>
/// <param name="node"></param>
private int idelcount = 0;
private void DeleteNodeToDB(TreeNode node……
[/Quote]

要是sql的话,那里怎么写呢? 直接根据ID删除?
Daqing 2011-05-18
  • 打赏
  • 举报
回复
        /// <summary>
/// 删除节点
/// </summary>
/// <param name="node"></param>
private int idelcount = 0;
private void DeleteNodeToDB(TreeNode node)
{

TreeNode currentnode = new TreeNode();
currentnode=node;
if (currentnode != null)
{
ZBZone zb = (ZBZone)((ZBZone)currentnode.Tag);
idelcount += DBManagement.Delete(zb.CodeID); //DBManagement.Delete这是删除方法,我封装了,如果你写sql语句,你可以直接在这写,执行就可。
}
if (currentnode.Nodes.Count > 0)
{
for (int j = 0; j < currentnode.Nodes.Count; j++)
{
DeleteNodeToDB(currentnode.Nodes[j]);
}
}

}

DeleteNodeToDB(treeview_customer.SelectedNode);调用
MessageBox.Show(string.Format("删除{0}条记录",idelcount));
treeview_customer.SelectedNode.Remove();

huangwenquan123 2011-05-18
  • 打赏
  • 举报
回复
或者你可以加个深度字段depth
id=2的父节点是1,那depth=,1,
id=3的父节点是1, 那depth=,1,
id=4的父节点是2, 那depth=,1,2,
...
然后根据深度删除
charindex(','+convert(varchar(200),ID)+',',','+@ID+',')
huangwenquan123 2011-05-18
  • 打赏
  • 举报
回复

create table #temp(id int identity(1,1) primary key,pid int,name varchar(20))
insert into #temp
select 0,'A' union all
select 1,'A1' union all
select 1,'A2' union all
select 2,'A11' union all
select 0,'B'

select * from #temp
declare @id int
set @id = 1
;with cte as(
select * from #temp where id=@id
union all
select t.* from #temp t inner join cte c on c.id=t.pid
)
delete from #temp where id in (select id from cte)
select * from #temp
drop table #temp
/*
删除前
1 0 A
2 1 A1
3 1 A2
4 2 A11
5 0 B

删除后
5 0 B
*/
bdmh 2011-05-18
  • 打赏
  • 举报
回复
这根几层没关系吧,你可以在数据库中设置级联删除,这样你只要删除一个,就会自动删除子集
实现Android端的简易思维导图。可以保存数据。编辑树形图。建立模型主要模型结构相对简单:TreeModel,NoteModel,NoteView,TreeView。核心实现分布如下:2017-07-01TreeModel:树形结构的存储,树形结构的遍历,添加、删除节点;NoteModel:节点关联的指向,和Parent的指向;TreeView :绘制树形结构,对树形结构位置的纠正,实现View层的添加,删除,note关联绘制;NoteView:显示text;编写位置计算核心代码在核心代码中,我想和大家分享的是TreeView如何对多种Style(树形形状)进行适配的问题。因为我们的树形结构的表达多种的,有的是一个半树形图,有点是圆形展开的等。对于这个问题,作为程序员如何进行解耦能,采用Interface进行解构适配,统一行为。所以在这里我写了一个TreeLayoutManager进行管理树形的位置表达。这里我实现了一个RightTreeLayoutManager。代码概况如下:接口public interface TreeLayoutManager {     /**      * 进行树形结构的位置计算      */     void onTreeLayout(TreeView treeView);     /**      * 位置分布好后的回调,用于确认ViewGroup的大小      */     ViewBox onTreeLayoutCallBack();     /**      * 修正位置      *      * @param treeView      * @param next      */     void correctLayout(TreeView treeView, NodeView next); }实现public class RightTreeLayoutManager implements TreeLayoutManager{     final int msg_standard_layout = 1;     final int msg_correct_layout = 2;     final int msg_box_call_back = 3;     private ViewBox mViewBox;     private int mDy;     private int mDx;     private int mHeight;     public RightTreeLayoutManager(int dx, int dy, int height) {         mViewBox = new ViewBox();         this.mDx = dx;         this.mDy = dy;         this.mHeight = height;     }     @Override     public void onTreeLayout(final TreeView treeView) {         final TreeModel mTreeModel = treeView.getTreeModel();         if (mTreeModel != null) {             View rootView = treeView.findNodeViewFromNodeModel(mTreeModel.getRootNode());             if (rootView != null) {                 rootTreeViewLayout((NodeView) rootView);             }             mTreeModel.addForTreeItem(new ForTreeItem() {                 @Override                 public void next(int msg, NodeModel next) {                     doNext(msg, next, treeView);                 }             });             //基本布局             mTreeModel.ergodicTreeInWith(msg_standard_layout);   
内容:   Asp.net页面内传参数方法   调用存储过程(两种方法比较)   调用存储过程通用类DBHelper   加密解密   SQL常用DBHelper   Asp.net存储过程无限分类   TreeView无限分类   无限分类MVC   荧光棒效果获取控件上全选   回车转换成Tab   DataGrid超级连接列   DataGrid行随鼠标变色   数字格式化   日期格式化   打开新的窗口并传送参数   为按钮添加对话框   删除表格选定记录   删除表格记录警告   关于日期格式   表格超连接列传递参数   清空Cookie   获取错误信息并到指定页面   自定义异常处理   javascript小技巧   DotNet密码加密的技术   用户MD5加密   ASP.net验证码实现   图片水印   防盗链   .NET(C#)连接各类数据库   读取配置文件数据库连接串   正则表达式验证   递归把十进制转换八进制(面试题目)   Mysql测试连接(DLL的引用)(插入与读取)   Asp.Net编辑器Fckeditor的使用   Asp.Net中FileUpload上传文件   Asp.Net中WebServices的使用   Asp.net手动绑定数据(分页、编辑、删除、加控件等操作)   Asp.net中My97DatePicker4.2日期的使用   Asp.net中DataList控件添加删除   Asp.net获取请求的用户信息IP地址   获取世界IP地址库显所在城市信息   MySQL事务处理   SQL内部常用函数
不断更新中  Asp.net页面内传参数方法  调用存储过程(两种方法比较)  调用存储过程通用类DBHelper  加密解密  SQL常用DBHelper  Asp.net存储过程无限分类  TreeView无限分类  无限分类MVC  荧光棒效果 获取控件上全选  回车转换成Tab  DataGrid超级连接列  DataGrid行随鼠标变色  数字格式化  日期格式化  打开新的窗口并传送参数  为按钮添加对话框  删除表格选定记录  删除表格记录警告  关于日期格式  表格超连接列传递参数  清空Cookie  获取错误信息并到指定页面  自定义异常处理  javascript小技巧  DotNet 密码加密的技术  用户MD5加密  ASP.net验证码实现  图片水印  防盗链  .NET(C#)连接各类数据库  读取配置文件数据库连接串  正则表达式验证  递归把十进制转换八进制(面试题目)  Mysql测试连接(DLL的引用)(插入与读取)  Asp.Net编辑器Fckeditor的使用  Asp.Net中FileUpload上传文件  Asp.Net中WebServices的使用  Asp.net手动绑定数据(分页、编辑、删除、加控件等操作)  Asp.net中My97DatePicker4.2日期的使用  Asp.net中DataList控件添加删除  Asp.net获取请求的用户信息IP地址  获取世界IP地址库显所在城市信息  MySQL事务处理  SQL内部常用函数  为gridview“删除”列添加确认对话框

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧