TreeView-demo

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using test1.ItemBase;      //添加引用
using System.Data.SqlClient;

namespace test1
{
    public partial class treeTest : Form
    {
        public treeTest()
        {
            InitializeComponent();
        }
        SqlBaseClass G_SqlClass = new SqlBaseClass();  //声明数据库操作类的对象
        private void InitTree()
        {
            string str = "select * from T_DEPARTMENT";
            DataSet ds = G_SqlClass.GetDs(str);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                TreeNode depNode = new TreeNode(row["DEPARTMENTNAME"].ToString());
                string userStr = "select * from T_USER where DEPARTMENTID="+row["ID"];
                DataSet ds1 = G_SqlClass.GetDs(userStr);
                foreach (DataRow row1 in ds1.Tables[0].Rows)
                {
                    TreeNode userNode = new TreeNode();
                    userNode.Name = row1["ID"].ToString();
                    userNode.Text = row1["USERNAME"].ToString();
                    depNode.Nodes.Add(userNode);
                }
                treeView1.Nodes.Add(depNode);
            }
        }

        private void treeTest_Load(object sender, EventArgs e)
        {
            InitTree();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;
            if (node.Level == 1)
            {
                this.dataGridView1.DataSource = this.getUserInfo(int.Parse(node.Name)).Tables[0];
            }
            if (node.Level == 0)
            {
                this.dataGridView1.DataSource = null;
            }
        }

        private DataSet getUserInfo(int userId)
        {
            string str = "select * from T_USER left join T_DEPARTMENT on T_USER.DEPARTMENTID=T_DEPARTMENT.ID where T_USER.ID=" + userId;
            DataSet ds = G_SqlClass.GetDs(str);
            //string name = ds.Tables[0].Rows[0][1].ToString();
            //MessageBox.Show(name);
            return ds;
        }
    }
}

 

 

你可能感兴趣的:(windows)