20151124002 treeView 数型菜单的操作

20151124002 treeView 数型菜单的操作

protected void FillTree()
        {
            SqlConnection1 = new System.Data.SqlClient.SqlConnection();
            SqlConnection1.ConnectionString = str_connect_88;
            string sql_text1 = " select distinct [per_dpt] FROM [kqdata].[dbo].[personal] ";
            this.SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(sql_text1, SqlConnection1);

this.DataSet1 = new System.Data.DataSet();
            this.SqlConnection1.Open();
            this.SqlDataAdapter1.Fill(DataSet1, "personal");
            this.SqlConnection1.Close();

Int32 int_count = DataSet1.Tables[0].Rows.Count;

TreeNode tmp;
            tmp = new TreeNode("xx公司");
            treeView1.Nodes.Add(tmp);
            treeView1.SelectedNode = tmp;

//dgv_from.DataSource = DataSet1.Tables[0];
            for (int i = 0; i < int_count; i++)
            {
                TreeNode tn = new TreeNode();
                tn.Text = DataSet1.Tables[0].Rows[i]["per_dpt"].ToString();
                treeView1.SelectedNode.Nodes.Add(tn);

// 2
                string sql_text2 = " select distinct [per_zhu] FROM [kqdata].[dbo].[personal] where [per_dpt] = '" + tn.Text + "' ";
                this.SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(sql_text2, SqlConnection1);

this.DataSet2 = new System.Data.DataSet();
                this.SqlConnection1.Open();
                this.SqlDataAdapter1.Fill(DataSet2, "personal");
                this.SqlConnection1.Close();

treeView1.SelectedNode = tn;
                Int32 int_count2 = DataSet2.Tables[0].Rows.Count;
                if (int_count2 > 0)
                {
                    for (int i2 = 0; i2 < int_count2; i2++)
                    {
                        TreeNode tn2 = new TreeNode();
                        tn2.Text = DataSet2.Tables[0].Rows[i2]["per_zhu"].ToString();
                        if (tn2.Text.Trim() != "")
                        {
                            treeView1.SelectedNode.Nodes.Add(tn2);
                        }

treeView1.SelectedNode = tn;

// 3
                        string sql_text3 = " select distinct [per_ban] FROM [kqdata].[dbo].[personal] where [per_zhu] = '" + tn2.Text + "' ";
                        this.SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(sql_text3, SqlConnection1);

this.DataSet3 = new System.Data.DataSet();
                        this.SqlConnection1.Open();
                        this.SqlDataAdapter1.Fill(DataSet3, "personal");
                        this.SqlConnection1.Close();

treeView1.SelectedNode = tn2;
                        Int32 int_count3 = DataSet3.Tables[0].Rows.Count;
                        if (int_count3 > 0)
                        {
                            for (int i3 = 0; i3 < int_count3; i3++)
                            {
                                TreeNode tn3 = new TreeNode();
                                tn3.Text = DataSet3.Tables[0].Rows[i3]["per_ban"].ToString();
                                if (tn3.Text.Trim() != "")
                                {
                                    treeView1.SelectedNode.Nodes.Add(tn3);
                                }

treeView1.SelectedNode = tn2;
                            }
                        }

treeView1.SelectedNode = tn;

}
                }

treeView1.SelectedNode = tmp;

//
            }

//展开组件中的所有节点
            treeView1.SelectedNode.ExpandAll();
            //定位根节点
            treeView1.SelectedNode = treeView1.Nodes[0];
        }

private void Frm_Main_Load(object sender, EventArgs e)
        {
            FillTree();
        }

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByMouse)//判断是否由鼠标触发的
            {
                if (treeView1.SelectedNode != null)
                {
                    MessageBox.Show(treeView1.SelectedNode.Text);
                }
            }
        }

上一篇:避免Castle Windsor引起的内存泄露


下一篇:用运算符代替if、else