treeview 绑定 datatable 或 dataset 添加节点

该博客展示了如何在C# WinForms应用程序中将数据库中的数据(使用DataTable和Dataset)绑定到TreeView控件,以创建一个显示数据的树形结构。通过执行SQL查询获取数据,然后遍历数据填充TreeNode,实现数据的可视化展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

namespace treeview2
{
    public partial class Form1 : Form
    {
        private static string connString = "Data Source=.;Initial Catalog=***;Integrated Security=True";
        public static SqlConnection connection = new SqlConnection(connString);
        private DataTable datatable;
        SqlDataAdapter dataAdapter;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindTree();
        }

        private DataTable getTreeViewData()
        {
            string sql = "select no,title from b_table";
            try
            {
                datatable = new DataTable();
                dataAdapter = new SqlDataAdapter(sql, connection);
                dataAdapter.Fill(datatable);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return datatable;
        }


        private void BindTree()
        {
            datatable = getTreeViewData();
            TreeNode allNode = new TreeNode("全部节点");
            allNode.Name = "node1";
            this.treeView1.Nodes.Add(allNode);
            for (int i = 0; i < datatable.Rows.Count; i++) {
                TreeNode temp_Node = new TreeNode(datatable.Rows[i]["title"].ToString());
                temp_Node.Name = "node2";
                allNode.Nodes.Add(temp_Node);
            }
                this.treeView1.Nodes[0].Expand();
        }

    }

}



namespace TreeView1
{
    public partial class Form1 : Form
    {
        private static string connString = "Data Source=.;Initial Catalog=***;Integrated Security=True";
        public static SqlConnection connection = new SqlConnection(connString);
        private DataSet dataset;
        SqlDataAdapter dataAdapter;


        public Form1()
        {
            InitializeComponent();
            //this.treeView1.LabelEdit = true;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            BindTree();
        }


        private DataSet getTreeViewData() {
            string sql = "select no,title from b_table";
            try
            {
                dataset = new DataSet();
                dataAdapter = new SqlDataAdapter(sql,connection);
                dataAdapter.Fill(dataset,"table1");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally {
                connection.Close();
            }
            return dataset;
        }


        private void BindTree() {
            dataset = getTreeViewData();
            TreeNode allNode = new TreeNode("所有节点");
            allNode.Name = "node1";
            this.treeView1.Nodes.Add(allNode);
            foreach (DataRow temp_Row in dataset.Tables["table1"].Rows) {
                TreeNode temp_Node = new TreeNode(temp_Row["title"].ToString());
                temp_Node.Name = "node2";
                allNode.Nodes.Add(qtemp_Node);
            }
            this.treeView1.Nodes[0].Expand();
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值