Windows 窗体应用程序从数据库表加载树并将其显示在 TreeView 对象中?

发布于 2024-11-01 20:57:17 字数 1870 浏览 0 评论 0原文

这是我到目前为止的代码,我的循环有问题,我不明白。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11_TreeView
{
    public partial class Form1 : Form
    {
        OleDbConnection dbConn;
        public Form1()
        {
            InitializeComponent();
            string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0; DataSource=PartsTree.accdb";
            try
            {
                dbConn = new OleDbConnection(connStr);
                dbConn.Open();
                AddChildNodes(treeView1.Nodes, 0);
                dbConn.Close();
                dbConn.Dispose();
            }
            catch (OleDbException e)
            {
                MessageBox.Show(e.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }

        private void AddChildNodes(TreeNodeCollection nodes, int parent)
        {
            string queryStr = "SELECT ID, parent_ID, description";
            queryStr += "FROM parts Where parent_ID";
            queryStr += (0 == parent ? "IS NULL;" : "=?");
            OleDbCommand dbCmd = dbConn.CreateCommand();
            dbCmd.CommandText = queryStr;

            if (0 != parent)
            {
                OleDbParameter parameter = dbCmd.Parameters.Add("@InputParm", OleDbType.Integer);
                parameter.Value = parent;
            }

            using (OleDbDataReader rdr = dbCmd.ExecuteReader())
            {
                while (rdr.Read())
                {

                }
                rdr.Close();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

This is the Code I have so far, I am having problem with the loop, I don't get it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11_TreeView
{
    public partial class Form1 : Form
    {
        OleDbConnection dbConn;
        public Form1()
        {
            InitializeComponent();
            string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0; DataSource=PartsTree.accdb";
            try
            {
                dbConn = new OleDbConnection(connStr);
                dbConn.Open();
                AddChildNodes(treeView1.Nodes, 0);
                dbConn.Close();
                dbConn.Dispose();
            }
            catch (OleDbException e)
            {
                MessageBox.Show(e.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }

        private void AddChildNodes(TreeNodeCollection nodes, int parent)
        {
            string queryStr = "SELECT ID, parent_ID, description";
            queryStr += "FROM parts Where parent_ID";
            queryStr += (0 == parent ? "IS NULL;" : "=?");
            OleDbCommand dbCmd = dbConn.CreateCommand();
            dbCmd.CommandText = queryStr;

            if (0 != parent)
            {
                OleDbParameter parameter = dbCmd.Parameters.Add("@InputParm", OleDbType.Integer);
                parameter.Value = parent;
            }

            using (OleDbDataReader rdr = dbCmd.ExecuteReader())
            {
                while (rdr.Read())
                {

                }
                rdr.Close();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大姐,你呐 2024-11-08 20:57:17

这是递归方法的示例:

//Link this to the AfterCheck property
private void treeViewCheckedChange(Object sender, TreeViewEventArgs e)
{
    TreeNode node = (TreeNode)e.Node;
    checkedNodes(node);
}

//Recursive method checks child, and then calls itself
private void checkedNodes(TreeNode parent)
{
    foreach (TreeNode child in parent.Nodes)
    {
        child.Checked = parent.Checked;
        checkedNodes(child);
    }
}

This is an example of a recursive method:

//Link this to the AfterCheck property
private void treeViewCheckedChange(Object sender, TreeViewEventArgs e)
{
    TreeNode node = (TreeNode)e.Node;
    checkedNodes(node);
}

//Recursive method checks child, and then calls itself
private void checkedNodes(TreeNode parent)
{
    foreach (TreeNode child in parent.Nodes)
    {
        child.Checked = parent.Checked;
        checkedNodes(child);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文