首页 > 开发 > 综合 > 正文

用TreeView显示数据通用方法

2024-07-21 02:16:09
字体:
来源:转载
供稿:网友

public void treeviewdata(dataset ds,string    textcolumnname,string parentcolumnname,string tagcolumnname)

{

 mytreedata root = new mytreedata();
    mytreedata child = null;
    for (i = 0 ; i < ds.tables[0].rows.count; ++i)
    {
     child = new mytreedata(ds.tables[0].rows[i][textcolumnname].tostring(),ds.tables[0].rows[i][tagcolumnname],ds.tables[0].rows[i][parentcolumnname]);
     root.add(child);
    }

    root.showtree(tv);
}



 /// <summary>
 /// 树型节点数据类
 /// </summary>
 public class mytreedata
 {
  public mytreedata()
  {
  }


  public mytreedata(string name)
  {
   name = name;
  }


  public mytreedata(string name,object tag,object parent)
  {
   name = name;
   tag = tag;
   parent = parent;
  }


  /// <summary>
  /// 子数据集合
  /// </summary>
  private mytreedatacollection child = new mytreedatacollection();


  /// <summary>
  /// 数据对象
  /// </summary>
  public object tag = null;

  /// <summary>
  /// 名称
  /// </summary>
  public string name = "";

  /// <summary>
  /// 父亲
  /// </summary>
  public object parent = null;


  /// <summary>
  /// 添加子数据
  /// </summary>
  /// <param name="child">子数据</param>
  public void add(mytreedata child)
  {
   int i;
   for (i =  this.child.count - 1; i >= 0;--i)
   {
    if (convert.toint32(this.child[i].parent) == convert.toint32(child.tag) )
    {
     child.child.add(this.child[i]);
     this.child.removeat(i);
    }
   }
   mytreedata parent = findparent(this,child);
   if (parent == null)
   {
    this.child.add(child);
   }
   else
   {
    parent.child.add(child);
   }
  }


  /// <summary>
  /// 查找父亲数据
  /// </summary>
  /// <param name="child">子数据</param>
  /// <returns>父亲数据或者null</returns>
  public mytreedata findparent(mytreedata parent,mytreedata child)
  {
   int i;
   for (i = 0; i < parent.child.count; ++i)
   {
    if (convert.toint32(parent.child[i].tag) == convert.toint32(child.parent))
    {
     return parent.child[i];
    }
    mytreedata temp = findparent(parent.child[i],child);
    if (temp != null)
    {
     return temp;
    }
   }
   return null;
  }


  /// <summary>
  /// 将子数据显示到treeview
  /// </summary>
  /// <param name="tv">treeview</param>
  public void showtree(treeview tv)
  {
   int i;
   treenode tn = null;
   for (i = 0 ; i < this.child.count; ++i)
   {
    tn = tv.nodes.add(this.child[i].name);
    tn.tag = this.child[i].tag;
    showtree(tn,this.child[i]);
   }
  }


  /// <summary>
  /// 递归显示数据
  /// </summary>
  /// <param name="tn">节点</param>
  /// <param name="child">数据</param>
  private void showtree(treenode tnparent,mytreedata child)
  {
   int i;
   treenode tn = null;
   for (i = 0 ; i < child.child.count; ++i)
   {
    tn = tnparent.nodes.add(child.child[i].name);
    tn.tag = child.child[i].tag;
    showtree(tn,child.child[i]);
   }
  }
   

  /// <summary>
  /// 树型节点集合类
  /// </summary>
  internal class mytreedatacollection : addcollectionbase
  {
   public mytreedatacollection()
   {
   }
    
   public mytreedata this[int index]
   {
    get
    {
     return (mytreedata)list[index];

    }
   }
  }
 }



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表