原创  用TreeView显示数据通用方法 收藏

 /// <summary>
 /// 树型节点数据类
 /// </summary>
 public class myTreeData
 {

  #region 构造方法

  public myTreeData()
  {
  }

  public myTreeData(string name)
  {
   Name = name;
  }

  public myTreeData(string name,object tag,object parent)
  {
   Name = name;
   Tag = tag;
   Parent = parent;
   if (Parent == DBNull.Value)
   {
    Parent = null;
   }
  }

  #endregion

  #region 其它

  /// <summary>
  /// 树型节点集合类
  /// </summary>
  internal class myTreeDataCollection : AddCollectionBase
  {
   public myTreeDataCollection()
   {
   }
    
   public myTreeData this[int index]
   {
    get
    {
     return (myTreeData)List[index];

    }
   }
  }

  #endregion

  #region 私有字段

  /// <summary>
  /// 子数据集合
  /// </summary>
  private myTreeDataCollection Child = new myTreeDataCollection();

  #endregion

  #region 公共方法

  /// <summary>
  /// 添加子数据
  /// </summary>
  /// <param name="child">子数据</param>
  public void Add(myTreeData child)
  {
   int i;
   for (i =  this.Child.Count - 1; i >= 0;--i)
   {
    if (this.Child[i].Parent == null)
    {
     continue;
    }
    if ( Convert.ToInt32(this.Child[i].Parent) == Convert.ToInt32(child.Tag) )
    {
     child.Child.Add(this.Child[i]);
     this.Child.RemoveAt(i);
    }
   }
   if (child.Parent == null )
   {
    this.Child.Add(child);
   }
   else
   {
    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;
    if (tv.ImageList != null)
    {
     tn.ImageIndex = 0;
    }
    ShowTree(tn,this.Child[i]);
   }
  }

  /// <summary>
  /// 将子数据显示到TreeView
  /// </summary>
  /// <param name="tv">TreeView</param>
  public void ShowTree(TreeNode tnParent)
  {
   int i;
   TreeNode tn = null;
   for (i = 0 ; i < this.Child.Count; ++i)
   {
    tn = tnParent.Nodes.Add(this.Child[i].Name);
    tn.Tag = this.Child[i].Tag;
    if (tnParent.TreeView.ImageList != null)
    {
     tn.ImageIndex = tnParent.ImageIndex < tnParent.TreeView.ImageList.Images.Count - 1 ? tnParent.ImageIndex + 1: tnParent.ImageIndex;
    }
    ShowTree(tn,this.Child[i]);
   }
  }

  #endregion

  #region 公共字段

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

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

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

  #endregion

  #region 私有方法

  /// <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;
    if (tnParent.TreeView.ImageList != null)
    {
     tn.ImageIndex = tnParent.ImageIndex < tnParent.TreeView.ImageList.Images.Count - 1 ? tnParent.ImageIndex + 1: tnParent.ImageIndex;
    }
    ShowTree(tn,child.Child[i]);
   }
  }

  #endregion

 }

 /// <summary>
 /// 提供增加接口的集合类
 /// </summary>
 public class AddCollectionBase : CollectionBase
 {
  public int Add(object obj)
  {
   return List.Add(obj);
  }
 }

发表于 @ 2005年04月15日 12:29:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:C#用鼠标右键选择TabPage | 新一篇:一个组合计算类

  • 发表评论
  • 评论内容:
  •  
Copyright © JasonHeung
Powered by CSDN Blog