简体   繁体   English

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

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

Whenever I tried to get the value from treeview in which I get the data from database on Get Data button and try to display it on datagridview by checking the checkbox of treeview node it will not display the details of that node in datagridview on Search button.每当我尝试从treeview获取值时,我在Get Data按钮上从数据库获取数据,并尝试通过选中treeview节点的复选框将其显示在datagridview上,它不会在Search按钮上的datagridview中显示该节点的详细信息。

Treeview after getting the data from Database on Get Data Button: TreeviewGet Data按钮上从数据库获取数据后:

在此处输入图像描述

So, I want now is that whenever I check the treeview node and then click on Search button it must show all details of that node in datagridview .所以,我现在想要的是,每当我检查treeview节点然后单击“ Search按钮时,它必须在datagridview中显示该节点的所有详细信息。

My Code of this Winform :我的这个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;
            }
        }
    }
}

This is the code in which I want to get the details of check node but I cannot get the details of that node.这是我想要获取检查节点详细信息但无法获取该节点详细信息的代码。

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;
        }
}

Database Table Product_Item :数据库表Product_Item

在此处输入图像描述

The TreeView.CheckBoxes Property returns a Boolean telling you whether the treeview displays checkboxes. TreeView.CheckBoxes 属性返回一个 Boolean 告诉您 treeview 是否显示复选框。 You cannot get the required SQL condition out of it, but you can get the item numbers of the checked items with您无法从中获得所需的 SQL 条件,但您可以使用

var itemNums = new List<string>();
foreach (TreeNode node in treeView1.Nodes)
{
    if (node.Checked) {
        itemNums.Add(node.Text);
    }
}

Now, you must create a SQL condition from this list.现在,您必须从此列表创建一个 SQL 条件。 Assuming that these numbers are stored in a int column, we can create a list of numbers with假设这些数字存储在一个int列中,我们可以创建一个数字列表

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

and then use this to build the SQL statement:然后使用它来构建 SQL 语句:

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

If, however, the item numbers are stored in a text column, then we must write但是,如果项目编号存储在文本列中,那么我们必须写

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

This produces conditions looking like WHERE ItemNo IN (1, 7, 9) or这会产生类似于WHERE ItemNo IN (1, 7, 9)
WHERE ItemNo IN ('1', '7', '9') . WHERE ItemNo IN ('1', '7', '9')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM