Tabel数据转换成TreeView

详解:

方法

 
public void GetTreeViewModel() { CityModle rtn = null; // ID string id; //名称 string name; //父节点全名 string pname; //父节点ID string pid; string sql = "SELECT [id],[name],[pid],[pname] FROM [Test].[dbo].[city]"; SqlDataReader dr = null; dr = DBHelper.GetReader(sql);
Dictionary<string, CityModle> cityDic = new Dictionary<string, CityModle>(); //遍历数据源 整理数据并放在 Dictionary中 while (dr.Read()) { id = dr["id"].ToString(); name = dr["name"].ToString(); pid = dr["pid"].ToString(); pname = dr["pname"].ToString(); CityModle city = new CityModle(id, name, pid, pname); cityDic.Add(id, city); } foreach (KeyValuePair<string, CityModle> tmp in cityDic) //遍历dictionary要使用KeyValuePair. 将Node链接成Tree. { if (cityDic.ContainsKey(tmp.Value.Pid)) { cityDic[tmp.Value.Pid].Add(tmp.Value); } if (tmp.Value.Pid == "10") //判断根节点,要根据实际的数据判断 { rtn = tmp.Value; } } this.treeView1.Nodes.Add(rtn.TreeNode); }
//使用到的Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
    class CityModle
    {
        private string id;
        private string name;
        private string pid;
        private string pname;
        private TreeNode node;

        public TreeNode TreeNode
        {
            get
            {
                return this.node;
            }
        }
        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Pid
        {
            get { return pid; }
            set { pid = value; }
        } 

        public string Pname
        {
            get { return pname; }
            set { pname = value; }
        }

       //构造函数
        public CityModle(string id, string name, string pid ,string pname)
        {
            this.Id = id;
            this.Name = name;
            this.Pid = pid;
            this.Pname = pname;
            this.node = new TreeNode(); 
            this.node.Text = this.name;
        }
        public bool Add(CityModle item)  //主要方法
        {
            if (this.Id.Equals(item.Pid))
            {
                this.node.Nodes.Add(item.TreeNode);

                return true;
            }
            else
            {
                return false;
            }

        }

    }
}

  

public TreeNode GetRuibetsu()
{
return OledbHelper.GetWPSFTreeModel(WPSF1Sql.RuibetsuSelect, WPSF1Sql.RuibetsuName).TreeNode;
}

public static WPSFTreeModel GetWPSFTreeModel(string mdx, string titleName = null)
{  //WPSFTreeModel 为自定义的一个Model

WPSFTreeModel rtn = null;

取得数据
OleDbDataReader reader = GetReader(mdx);

//名称(缩写,可能重名)
string name;
//全名(唯一性,不能重名)
string unique;
//父节点全名(唯一性)
string parentUnique;

Dictionary<string, WPSFTreeModel> modelDic = new Dictionary<string, WPSFTreeModel>();

//遍历数据源

while (reader.Read())

{

name = reader.GetValue["列名"].ToString();
unique = reader.GetValue["列名"].ToString();
parentUnique = reader.GetValue["列名"].ToString();

WPSFTreeModel item = new WPSFTreeModel(name, unique, parentUnique);

if (modelDic.ContainsKey(unique))
{
modelDic[unique] = item;
}
else
{
modelDic.Add(unique, item);
}


if (modelDic.ContainsKey(parentUnique))
{
modelDic[parentUnique].Add(item);
}
else
{
modelDic.Add(parentUnique, null);//error?
}


if (string.IsNullOrEmpty(parentUnique))
{
rtn = item;
}
}


if (!string.IsNullOrEmpty(titleName))
{
rtn.TreeNode.Text = titleName;
}
rtn.TreeNode.CollapseAll();

return rtn;
}

//自定义Model

public class WPSFTreeModel
{
//构造函数
public WPSFTreeModel(string name,string unique,string parentUnique)

this.name = name;
this.unique = unique;
this.parentUnique = parentUnique;

this.node = new TreeNode(); ;
this.node.SelectAction = TreeNodeSelectAction.None;
this.node.Text = this.name;
this.node.Value = this.name;
}


private string name;

public string Name
{
  get
    {
      return this.name;
    }
}


private string unique;


public string Unique
{
   get
    {
      return this.unique;
    }
}

private string parentUnique;

public string ParentUnique
{
   get
    {
      return this.parentUnique;
    }
}

private TreeNode node;

public TreeNode TreeNode
{
  get
    {
      return this.node;
    }
}

//这个属性可以不要
private List<WPSFTreeModel> list;

public List<WPSFTreeModel> List
{
  get
    {
      return this.list;
    }
}

//这个方法可以不要
public void ClearList()
{
  this.list = null;
}

//添加子节点

public bool Add(WPSFTreeModel item)
{
  if(this.list==null)
    {
      this.list = new List<WPSFTreeModel>();
    }

//上面的可以不要
  if(this.unique.Equals( item.parentUnique))
    {
      this.list.Add(item);
      this.node.ChildNodes.Add(item.TreeNode);

      return true;
    }
  else
    {
      return false;
  }

}

//这个方法可以不要

private void AddRange(List<WPSFTreeModel> list)
{
   if (this.list == null)
    {
      this.list = new List<WPSFTreeModel>();
    }
  this.list.AddRange(list);
}
}

原文地址:https://www.cnblogs.com/loveLu/p/4904784.html