【问题标题】:Facing Problem in Tree View in C# windows form with details below:在 C# windows 窗体中的树视图中面临问题,详细信息如下:
【发布时间】:2021-09-03 07:00:19
【问题描述】:

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

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

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

我的这个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;
        }
}

数据库表Product_Item:

【问题讨论】:

    标签: c# winforms search treeview


    【解决方案1】:

    TreeView.CheckBoxes Property 返回一个布尔值,告诉您树视图是否显示复选框。您无法从中获取所需的 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') 的条件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2010-12-06
      • 1970-01-01
      • 2021-09-06
      • 2011-03-27
      • 1970-01-01
      • 2017-06-17
      相关资源
      最近更新 更多