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 点击树节点中id赋值到文本框中
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool IsPostBack { get; private set; }
private void Form1_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)//客服分类树表
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from dbo.ServiceClassifyTree", "server=WINDOWS-CBBU2E6;database=OrderManagementSystem;uid=liweilu;pwd=liweilu123;");
da.Fill(ds);
DataTable dt = ds.Tables["dbo.ServiceClassifyTree"]; //从数据库里取出数据
this.treeView1.Nodes.Clear();
nodes(this.treeView1.Nodes, ds, 0);
this.treeView1.Nodes[0].Expand();
}
}
private void nodes(TreeNodeCollection tnode, DataSet ds, int id)
{
//创建视图 ,应为只有一张表
DataView dview = new DataView(ds.Tables[0]);
//视图的好处,在于能方便筛选数据
dview.RowFilter = "[fid]=" + id + "";
//显示每个节点的标题,递归!
foreach (DataRowView row in dview)
{
TreeNode node = new TreeNode();
node.Text = row["name"].ToString();
tnode.Add(node);
nodes(node.Nodes, ds, int.Parse(row["id"].ToString()));
}
}
SqlConnection conn;
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = treeView1.SelectedNode.Text;//得到选中节点文本内容
textBox3.Text = treeView1.SelectedNode.Parent.Text.ToString();
textBox4.Text = treeView1.SelectedNode.Parent.Text;
conn = new SqlConnection("server=WINDOWS-CBBU2E6;database=OrderManagementSystem;Uid = sa; Pwd = 123");
SqlCommand cmd = new SqlCommand("select id from dbo.ServiceClassifyTree where name='" + textBox1.Text + "'", conn);
conn.Open();
SqlDataReader myReader = cmd.ExecuteReader();
while (myReader.Read())
{
textBox2.Text = myReader.GetValue(0).ToString();
}
myReader.Close();
conn.Close();
}
}
}