从SQL数据库中提取C#中的树视图 [英] Pobulate treeview in C# from sql data base

查看:80
本文介绍了从SQL数据库中提取C#中的树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有桌子

Id ParentId名字

1 0动物

2 1 Elephent

3 0人类

4 2 Jack

5 3 John







请帮我在树视图中对这个表进行分析

*使用WinForm C#4.00

I have table
Id ParentId Name
1 0 Animal
2 1 Elephent
3 0 Human
4 2 Jack
5 3 John



Please Help me to pobulate this table in treeview
* using WinForm C# 4.00

推荐答案

DataSet Ds = new DataSet();
           try {
               string strCon = @"Data Source=Servername;Initial Catalog=Test;Integrated Security=True";
               SqlConnection Con = new SqlConnection(strCon);
               SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
               Da.Fill(Ds);
           } catch (Exception) { }
DataRow[] Rows = ds.Tables[0].Rows; // Get all parents nodes
               for (int i = 0; i < Rows.Length; i++)
               {
                   TreeNode root = new TreeNode(Rows[i][0].ToString(), Rows[i]["ProductId"].ToString());
                   root.SelectAction = TreeNodeSelectAction.Expand;
                   CreateNode(root, ds.Tables[0]);
                   treeviwExample.Nodes.Add(root);
               }

 public void CreateNode(TreeNode node , DataTable Dt)
       {
           DataRow[] Rows = Dt.Select("ParentId =" + node.Value);
           if (Rows.Length == 0) { return; }
           for (int i = 0; i < Rows.Length; i++)
           {
               TreeNode Childnode = new TreeNode(Rows[i][0].ToString(), Rows[i][1].ToString());
               Childnode.SelectAction = TreeNodeSelectAction.Expand;
               node.ChildNodes.Add(Childnode);
               CreateNode(Childnode, Dt);
           }
       }


这篇关于从SQL数据库中提取C#中的树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆