标签:
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);
}
}
标签:
原文地址:http://www.cnblogs.com/loveLu/p/4904784.html