您的位置:首页 > 数据库

[C#]读取数据库,动态生成TreeView[递归]

2011-12-14 14:22 597 查看
    我自己的画图界面已经做完了,现在开始连接后台,引用WEB服务,然后读取数据库,有个地方要动态生成树,昨天弄了半天,结果怎么弄怎么是死循环,领导又去开会,结果钻进牛角尖就出不来了,还好今天早上领导一来看看我的代码,一眼就看出毛病了,我也恍然大悟,看来自己对数据机构和算法这些基础内容还有很大的欠缺啊,现在终于写出来, 把结果和大家分享,昨天上网看的时候发现有人和我一样的问题。

 

private void init() {
this.m_userId = 101;
this.comboBox1.Items.Clear();
this.treeView.Nodes.Clear();

CatalogNode rootNode = new CatalogNode("请选择分类");
rootNode.Id = 0;
this.treeView.Nodes.Add(rootNode);
this.showTreeView(rootNode);
this.treeView.Nodes[0].Expand();
}

private void showTreeView(CatalogNode parentNode) {
//从数据库中查寻结果
catalogBean[] cataArray = this.m_service.queryCatalogs(parentNode.Id, this.m_userId);
if(null == cataArray){
return;
}
for (int i = 0; i < cataArray.Length; i++) {
CatalogNode cn = new CatalogNode(cataArray[i].name);
cn.Id = cataArray[i].id;
cn.ParentId = cataArray[i].parentId;
cn.Description = cataArray[i].description;
this.comboBox1.Items.Add(cn.Name);
parentNode.Nodes.Add(cn);
}
foreach (CatalogNode cn in parentNode.Nodes) {
this.showTreeView(cn);
}
}


注:CatalogNode类是我自己写的一个继承自TreeNode的类,多了id(long) , parentId(long)和description(string)属性,用起来方便,没其他的。

service是公司自己研发的框架,面向对象的数据库操作~所以查出来的直接是数组~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 动态 treeview 曲沐阳