DBTreeView

using  System;
using  System.Collections;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Text;
using  System.Windows.Forms;

namespace  WindowsApplication1
{
    
internal   class  DBTreeView : TreeView
    {
        
private   object  rootValue;
        
private   string  parentMember;
        
private   string  childMember;
        
private   string  textMember;

        
private   object  dataSource;
        
private  CurrencyManager cm;

        
public   object  DataSource
        {
            
get  {  return  dataSource; }
            
set
            {
                
if  (value  ==   null )
                {
                    
this .cm  =   null ;
                    
// this.GroupingChanged();
                }
                
else
                {
                    
if  ( ! ((value  is  IList)  |  (value  is  IListSource)))
                    {
                        
throw   new  Exception( " Invalid DataSource " );
                    }
                    
if  (value  is  IListSource)
                    {
                        IListSource myListSource 
=  (IListSource)value;
                        
if  (myListSource.ContainsListCollection)
                        {
                            
throw   new  Exception( " Invalid DataSource " );
                        }
                        
this .dataSource  =  value;
                        
this .cm  =  (CurrencyManager) this .BindingContext[value];
                        
// this.GroupingChanged();
                    }
                    
else
                    {
                        
this .dataSource  =  value;
                        
this .cm  =  (CurrencyManager) this .BindingContext[value];
                        
// this.GroupingChanged();
                    }
                }
            }
        }

        [Category(
" Data " )]
        
public   string  TextMember
        {
            
get  {  return  textMember; }
            
set  { textMember  =  value; }
        }

        [Category(
" Data " )]
        
public   string  ChildMember
        {
            
get  {  return  childMember; }
            
set  { childMember  =  value; }
        }

        [Category(
" Data " )]
        
public   object  RootValue
        {
            
get  {  return   this .rootValue; }
            
set  {  this .rootValue  =  value; }
        }

        [Category(
" Data " )]
        
public   string  ParentMember
        {
            
get
            {
                
return   this .parentMember;
            }
            
set
            {
                
this .parentMember  =  value;
            }
        }

        
public   void  BuildTree()
        {
            
this .Nodes.Clear();

            
this .BuildTree( this .Nodes,  this .rootValue);
        }

        
private   void  BuildTree(TreeNodeCollection currNodes,  object  parentValue)
        {
            
if  (( this .cm  !=   null &&  ( this .cm.List  !=   null ))
            {
                IBindingListView bindingList 
=   this .cm.List  as  IBindingListView;
                
if  (bindingList  !=   null   &&  bindingList.SupportsFiltering)
                {
                    bindingList.Filter 
=   string .Format( " {0}='{1}' " this .parentMember, parentValue);
                }

                IList innerList 
=   this .cm.List;
                
int  currentListIndex  =   0 ;
                PropertyDescriptorCollection properties 
=   this .cm.GetItemProperties();
                PropertyDescriptor pdParentMember 
=  properties[ this .parentMember];
                PropertyDescriptor pdChildMember 
=  properties[ this .childMember];
                PropertyDescriptor pdTextMember 
=  properties[ this .textMember];
                
// TreeNodeCollection currNodes = this.Nodes;
                 while  (currentListIndex  <  innerList.Count)
                {
                    
object  currObject  =  innerList[currentListIndex];
                    TreeLeafNode myFirstNode 
=   new  TreeLeafNode();
                    myFirstNode.Text 
=  Convert.ToString(pdChildMember.GetValue(currObject));
                    myFirstNode.Value 
=  pdChildMember.GetValue(currObject);
                    myFirstNode.Item 
=  currObject;
                    
// this.BuildTree(myFirstNode.Nodes, pdChildMember.GetValue(currObject));
                    currNodes.Add(myFirstNode);

                    currentListIndex 
+=   1 ;
                }
                
for  ( int  i  =   0 ; i  <  currNodes.Count; i ++ )
                {
                    TreeLeafNode node 
=  currNodes[i]  as  TreeLeafNode;
                    
this .BuildTree(node.Nodes, node.Value);
                }
            }
        }

    }
}
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Windows.Forms;

namespace  WindowsApplication1
{
    
internal    class  TreeLeafNode : TreeNode
    {
        
private   int  position;
        
private   object  value;
        
private   object  item;

        
public   object  Item
        {
            
get  {  return  item; }
            
set  { item  =  value; }
        }

        
public   object  Value
        {
            
get  {  return   this .value; }
            
set  {  this .value  =  value; }
        }

        
public   int  Position
        {
            
get  {  return  position; }
            
set  { position  =  value; }
        }       
    }
}

转载于:https://www.cnblogs.com/leslie116/archive/2011/02/26/1965958.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
玩delphi,学pascal那么久了,很多知识也都是互联网上搜索,也是依靠很多网友提供的资料去学习的,在此感谢互联网, 感谢那些技术分享的朋友们,近来打算在鄙人这年纪最后完成一个二次开发框架的软件(全凭自己的兴趣,因本人就一IT宅男,搞完后 估计本人也没有精力学习其它了),此软件想尽量少用第三方控件,但里面的DBTree功能不可避免,本以为网上找个DBTree是个手到擒来 的事情,没想到百度了一整天,居然没有一个好的DBTree,也许有人会说Ehlib的dbgridEH+Memtableeh可以,但这第三方软件包还需要带 有其它的东西,有点大,前面说过了,本软件想尽量少用第三方,除此外,国内的DBTree控件居然没有几个行的,看来delphi真的落寞了, 只能祝福delphi借xe5跨平台再次崛起吧!其实也不是说国产的DBTree没有,有那么两三个人做过,但我测试后很多问题,有些还把SQL做 到控件里,而且关联死TADOQuery,这样的控件让人以后如何用,国外的DBTree目前就发现一个还可以的是Dxdbtree, 公司也是大名顶顶的Developer Express Inc ,但不知道为何,网上找的只有1.3.1版本,而且只是For D7的,装上后, 发现还是要引用自己的内存数据(MemData)表才行,看了下源码,写得很复杂,晕啊,我只想找个简简单单功能的DBTree控件,怎么那么难呢, 要求也不高,只需要继承于TTreeView,载入数据表树结构,展示出一个TreeView,能简单的拖动,删除树结构的操作就行, 求人不如求自,既然简单的就自己来做一个控件吧!经过三天参看源码和网络技术资料搜索,基本学会了delphi控件开发了, 因鄙人之前并没有真正做过什么控件,最后做出来的结果感觉还是不错的,鄙人在此公布源码并对源码关键地方用中文详细注释, 目的是希望给以后有类似像我这样的找DBTree的朋友一点帮助!同时也是给初学delphi控件编程的朋友是一个好教材!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值