温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# Winfrom Treeview树形结构使用

发布时间:2020-08-03 19:41:52 来源:网络 阅读:2662 作者:hellosmart 栏目:编程语言

using System;

using System.Data;

using System.Drawing;

using System.Windows.Forms;

using System.Data.SqlClient;


namespace TreeViewList

{

    public partial class Form1 : Form

    {

        DataTable dt = new DataTable();

        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            string conString = "Data Source=服务器名称;Initial Catalog=数据库名;User ID=登陆名;Pwd=密码";

            SqlConnection con = new SqlConnection(conString);

            con.Open();

            string strSql =  "select * from 数据库表名";

            SqlDataAdapter da = new SqlDataAdapter(strSql, con);

            da.Fill(dt);

            AddTreeNode(treeView1,0,null);

        }


        /// <summary>

        /// 树形TreeView绑定数据

        /// </summary>

        /// <param name="tv"></param>

        /// <param name="parentid"></param>

        /// <param name="pNode"></param>

        protected void AddTreeNode(TreeView tv,int parentid, TreeNode pNode)

        {

            foreach (DataRow dv in dt.Select("parentID="+parentid))

            {

                TreeNode node = new TreeNode();

                node.Text = dv["Name"].ToString();

                node.Tag = dv["ID"].ToString();            


                if (pNode == null)

                {

                    tv.Nodes.Add(node);

                }

                else

                {

                    pNode.Nodes.Add(node);

                }

                AddTreeNode(tv, Convert.ToInt32(dv["ID"].ToString()), node);

            }

        }


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI