在 C# windows 形式的树视图中面临问题,详情如下:

Facing Problem in Tree View in C# windows form with details below:

每当我尝试从 treeview 获取值时,我在 Get Data 按钮上从数据库获取数据,并尝试通过选中 datagridview 的复选框将其显示在 datagridview 上=13=]节点它不会在Search按钮上的datagridview中显示该节点的详细信息。

TreeviewGet Data 按钮上从数据库获取数据后:

所以,我现在想要的是,每当我检查 treeview 节点然后单击 Search 按钮时,它必须在 [=15= 中显示该节点的所有详细信息].

我的代码 Winform:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace My_Work
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Get_Data_Click(object sender, EventArgs e)
        {
            string conStr = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Data;Data Source=MY-PC";
            SqlConnection con = new SqlConnection(conStr);
            con.Open();
            string query = "select ItemNo,UnitePrice from Product_Item";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                TreeNode n = new TreeNode(sdr["ItemNo"].ToString());
                treeView1.Nodes.Add(n);
                n.Nodes.Add(sdr["UnitePrice"].ToString());
            }
            con.Close();
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string conStr = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Data;Data Source=MY-PC";
            SqlConnection con = new SqlConnection(conStr);
            con.Open();
            string nodeName = e.Node.ToString().Replace("TreeNode: ", string.Empty);
            if (e.Node.Parent != null)
            {
                string q = "select * from Product_Item where UnitePrice='" + nodeName + "'";
                SqlCommand cmd = new SqlCommand(q, con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                con.Close();
                dataGridView1.DataSource = dt;
            }
        }

        private void Search_Click(object sender, EventArgs e)
        {
            string conStr = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Data;Data Source=MY-PC";
            SqlConnection con = new SqlConnection(conStr);
            con.Open();
            if(treeView1.CheckBoxes == true)
            {
                string nodeCheck = treeView1.CheckBoxes.ToString();
                string q = "select * from Product_Item where ItemNo='" + nodeCheck + "'";
                SqlCommand cmd = new SqlCommand(q, con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                con.Close();
                dataGridView1.DataSource = dt;
            }
        }
    }
}

这是我想要获取检查节点详细信息但无法获取该节点详细信息的代码。

private void Search_Click(object sender, EventArgs e)
{
        string conStr = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Data;Data Source=MY-PC";
        SqlConnection con = new SqlConnection(conStr);
        con.Open();
        if(treeView1.CheckBoxes == true)
        {
            string nodeCheck = treeView1.CheckBoxes.ToString();
            string q = "select * from Product_Item where ItemNo='" + nodeCheck + "'";
            SqlCommand cmd = new SqlCommand(q, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }
}

数据库TableProduct_Item:

TreeView.CheckBoxes Property returns 一个布尔值,告诉您树视图是否显示复选框。您无法从中获取所需的 SQL 条件,但您可以使用

获取已检查项目的项目编号
var itemNums = new List<string>();
foreach (TreeNode node in treeView1.Nodes)
{
    if (node.Checked) {
        itemNums.Add(node.Text);
    }
}

现在,您必须根据此列表创建一个 SQL 条件。假设这些数字存储在 int 列中,我们可以创建一个数字列表

string numList = String.Join(", ", itemNums);

然后使用它来构建 SQL 语句:

string sql = "SELECT * FROM Product_Item WHERE ItemNo IN (" + numList  + ")";

但是,如果项目编号存储在文本列中,那么我们必须写

string numList = String.Join("', '", itemNums);
string sql = "SELECT * FROM Product_Item WHERE ItemNo IN ('" + numList  + "')";

这会产生类似 WHERE ItemNo IN (1, 7, 9)
的条件 WHERE ItemNo IN ('1', '7', '9').