如何在 C# 控制台中填充树视图 Visual Studio 2012 中的两个或多个表

本文关键字:两个 2012 Studio 控制台 填充 Visual 视图 | 更新日期: 2023-09-27 18:31:38

我的问题是:如何在Visual Studio 2012中从多个表中填充树视图,然后根据我在treeView中的每个节点中的选择使用treeView_afterSelect_event进行DataGridView更改?

我知道如何一次插入一个表,但我不知道如何在 C# 控制台中部署以下查询:我的数据库中有两个表,分别名为 bom$ 和 part$。查询在 SQL Server 中成功运行。

SELECT bom$.PARENT_NAME, bom$.COMPONENT_NAME
FROM bom$
FULL OUTER JOIN part$
ON bom$.PARENT_NAME=part$.NAME
ORDER BY bom$.PARENT_NAME;

下面是来自 C# 控制台 Visual Studio 2012 的以下代码。也让我知道after_select_event。我刚刚完成了信息系统管理文凭的学习,所以我是新手;因此,任何帮助将不胜感激。

namespace QBuild_Test2
{
    public partial class Form1 : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=COMINSIDE-PC;Initial Catalog=QBuild_Software;User ID=sa;Password=aptech");
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Shown(object sender, EventArgs e)
        {
            try
            {
                conn.Open();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.ExitThread();
            }
        }
        private void populate_btn_Click(object sender, EventArgs e)
        {
            //on button click treeview control and datagrid view both will populate at the same time directly from the database
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = @"select PARENT_NAME,COMPONENT_NAME,PART_NUMBER,TITLE,QUANTITY,[TYPE],ITEM,Material FROM bom$ FULL OUTER JOIN part$ ON bom$.PARENT_NAME=part$.NAME ORDER BY bom$.PARENT_NAME;";
            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(dt);
            dataGridView.DataSource = dt;
            treeView.Nodes.Clear();
            SqlCommand cmd2 = new SqlCommand(@"select * from bom$ order by PARENT_NAME asc");
            cmd2.Connection = conn;
            // I'm able to populate one table in treeview not both tables
            try
            {
            SqlDataAdapter sda = new SqlDataAdapter(cmd2);
            DataTable dt2 = new DataTable();
            sda.Fill(dt2);
               foreach (DataRow dr in dt2.Rows)
                  {
                   TreeNode node = new TreeNode(dr["PARENT_NAME"].ToString());
                   node.Nodes.Add(dr["QUANTITY"].ToString());
                   node.Nodes.Add(dr["COMPONENT_NAME"].ToString());
                   treeView.Nodes.Add(node);
                  }
               conn.Close();
             }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
                //this will disable the populate button once the data populated
                populate_btn.Enabled = false;
        }
//this code is to populate result by selecting node based on the selection but failed.
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if(e.Node.Text != null)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = @"select * from bom$ order by PARENT_NAME ASC;";
                DataTable dt = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
                dataGridView.DataSource = dt.DefaultView;
            }
        }
    }
} 

如何在 C# 控制台中填充树视图 Visual Studio 2012 中的两个或多个表

看起来你在寻找这样的东西

http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/how-to-bind-multiple-sql-server-tables-with-a-treeview-in-a/