一个继承于DropDownList的树状控件

由于工作需要特扩展了一下已有的DropDownList控件,使其能显示树状结构的数据,主要用于那些有层次关系的数据显示。
如下图所示: demo1.jpg
该控件为无限级扩展的,只需要指定其子节点字段名(默认为ID),父节点字段名(默认为parentID),以及第一层父节点的值即可(默认为0)。

1.首先定义常量
None.gif protected   const   string  strT = " " ;
None.gif
protected   const   string  strL = " " ;
None.gif
protected   const   string  strI = " " ;
None.gif
protected  DataTable tempTable;
None.gif
protected   int  lay  =   0 ; // 节点层数
None.gif
protected   int  level = 0 ; // DropDownList顺序

2.添加属性
ContractedBlock.gif ExpandedBlockStart.gif 属性
None.gif[Category("数据"),Description("第一层父节点的值")]
None.gif        
public int FirstParentValue
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["FirstParentValue"];
InBlock.gif                
return ((obj==null)?0:(int)obj);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"FirstParentValue"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        [Category(
"数据"),Description("子字段名")]
None.gif        
public string ChildField
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["ChildField"];
InBlock.gif                
return ((obj==null)?"ID":(string)obj);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"ChildField"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        [Category(
"数据"),Description("父字段名")]
None.gif        
public string ParentField
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["ParentField"];
InBlock.gif                
return ((obj==null)?"parentID":(string)obj);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"ParentField"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        [Category(
"数据"),Description("显示文本字段名")]
None.gif        
public string ShowText
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["ShowText"];
InBlock.gif                
return ((obj==null)?"name":(string)obj);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"ShowText"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

3.计算当前节点所在的层以及是否为子节点,是否有兄弟节点等函数,用于生成树形
ContractedBlock.gif ExpandedBlockStart.gif 生成树形时要用的函数
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif        
/// 递归算出指定ID所在树中的层数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="treeTable"></param>
ExpandedBlockEnd.gif        
/// <param name="ID">节点ID</param>

None.gif        private void FindLay(DataTable treeTable,int ID) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
for(int i=0;i<treeTable.Rows.Count;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ID == int.Parse(treeTable.Rows[i][this.ChildField].ToString()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int parentID = int.Parse(treeTable.Rows[i][this.ParentField].ToString());
InBlock.gif     
InBlock.gif                    
// 如果父节点不是根节点,递归
InBlock.gif
                    if(parentID != 0
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        lay 
++;
InBlock.gif                        FindLay(treeTable,
int.Parse(treeTable.Rows[i][this.ParentField].ToString()));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
            
ExpandedBlockEnd.gif        }
        
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
InBlock.gif        
/// 判断是否有子接点
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dt"></param>
InBlock.gif        
/// <param name="ID">当前节点的ID</param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif        private bool IsExistChildNodes(DataTable dt,string ID)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
bool flag=false;
InBlock.gif            
if(ID==null)
InBlock.gif                
return flag;
InBlock.gif            
for(int i=0;i<dt.Rows.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(dt.Rows[i][this.ParentField].ToString()==ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    flag
=true;
InBlock.gif                    
return flag;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    flag
=false;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
InBlock.gif        
/// 判断是否有兄弟节点
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dt"></param>
InBlock.gif        
/// <param name="ID">当前节点的ID</param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif        private bool IsBorthorNodes(DataTable dt,string ID)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{            
InBlock.gif            
bool flag=false;
InBlock.gif            
if(ID==null)
InBlock.gif                
return flag;
InBlock.gif            
for(int i=0;i<dt.Rows.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(dt.Rows[i][this.ChildField].ToString()==ID&&i<dt.Rows.Count-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(dt.Rows[i][this.ParentField].ToString()==dt.Rows[i+1][this.ParentField].ToString())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        flag
=true;
InBlock.gif                        
return flag;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        flag
=false;
InBlock.gif                        
return flag;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }
              
InBlock.gif                  
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedBlockEnd.gif        }

None.gif        
ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
InBlock.gif        
/// 判断当前节点是否为子节点
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dt"></param>
InBlock.gif        
/// <param name="ID"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif        private bool IsChildNode(DataTable dt,string ID)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{        
InBlock.gif            
bool flag=false;
InBlock.gif            
if(ID==null)
InBlock.gif                
return flag;
InBlock.gif            
for(int i=0;i<dt.Rows.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(dt.Rows[i][this.ChildField].ToString()==ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(dt.Rows[i][this.ParentField].ToString()=="0")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{                      
InBlock.gif                        flag
=false;
InBlock.gif                        
return flag;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        flag
=true;
InBlock.gif                        
return flag;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedBlockEnd.gif        }

4.递归生成树形
ContractedBlock.gif ExpandedBlockStart.gif 递归生成树形
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif        
/// 数据添加并生成树形
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dt"></param>
ExpandedBlockEnd.gif        
/// <param name="ID"></param>

None.gif        private void BindData(DataTable dt,string ID)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            DataRow[] dr;
InBlock.gif            dr
=dt.Select(this.ParentField+"="+ID,this.ChildField+" ASC");            
InBlock.gif            
for(int x=0;x<dr.Length;x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{     
InBlock.gif                
string str="";  
InBlock.gif                lay
=0;
InBlock.gif                
this.FindLay(dt,int.Parse(dr[x][this.ChildField].ToString()));
InBlock.gif                
if(this.IsChildNode(dt,dr[x][this.ChildField].ToString())==true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    
for(int i=0;i<lay;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        str
+=strI;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if(this.IsBorthorNodes(dt,dr[x]["id"].ToString())==true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.Items.Insert(level,new ListItem(str+strT+dr[x][this.ShowText].ToString(),dr[x][this.ChildField].ToString()));
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                     
this.Items.Insert(level,new ListItem(str+strL+dr[x][this.ShowText].ToString(),dr[x][this.ChildField].ToString()));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Items.Insert(level,new ListItem(str+dr[x][this.ShowText].ToString(),dr[x][this.ChildField].ToString()));
ExpandedSubBlockEnd.gif                }
                
InBlock.gif                level
+=1;
InBlock.gif                
this.BindData(dt,dr[x][this.ChildField].ToString());//递归生成树
ExpandedSubBlockEnd.gif
            }

ExpandedBlockEnd.gif        }

5.绑定数据
ContractedBlock.gif ExpandedBlockStart.gif 绑定数据
ExpandedBlockStart.gifContractedBlock.gif /**//// <summary>
InBlock.gif        
/// 绑定数据
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="dt">数据源DataTable</param>

None.gif        public void BindListData(DataTable dt)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            tempTable
=dt.Clone();
InBlock.gif            
this.BindData(dt,this.FirstParentValue.ToString());
ExpandedBlockEnd.gif        }
使用的时候调用该函数进行数据绑定!

请大家多多指教以便改进,谢谢!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计,皆可应用在项目开发、毕业设计、课程设计、期末/期/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值