欢迎光临
我们一直在努力

用TreeView显示数据通用方法-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

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];

    }
   }
  }
 }

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用TreeView显示数据通用方法-.NET教程,Asp.Net开发
分享到: 更多 (0)