/// <summary>
 /// 地区类型下拉框改变索引时触发
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
 {
     try
     {
         int cbType = Convert.ToInt32(comboBox1.SelectedValue.ToString());
         if (cbType == 1)
         {
             treeView1.Nodes.Clear();
             DataTable dt = cm.SelCityTable();
             AddTree("", null, "", dt);  //加载节点数据
             return;
         }
         if (cbType == 2)
         {
             DataTable dt = pm.SelProfession();
             treeView1.Nodes.Clear();
             AddTree("", null, "P", dt);  //加载节点数据
             return;
         }
         else
         {
             MessageBox.Show("类型选择错误!");
             return;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("加载数据失败,请检查服务器连接并尝试刷新.错误:" + ex.Message);
     }
 }
Beispiel #2
0
        /// <summary>
        /// 递归添加树的节点
        /// </summary>
        /// <param name="ParentID">父级ID:默认为空</param>
        /// <param name="pNode">父级节点:默认为null,可选</param>
        /// <param name="table">表名:默认为City,可选参数:P</param>
        /// <param name="ControlName">控件名:必选</param>
        private void AddTree(string ParentID, Node pNode, string table, ComboTree ControlName)
        {
            if (ParentID == "")
            {
                ParentID = "D4";
            }
            string ParentId = "City_ParentId";
            string Code     = "City_Code";
            string Name     = "City_Name";

            try
            {
                DataTable dt = cm.SelCityTable();
                if (table == "P")
                {
                    ParentId = "ST_ParentId";
                    Code     = "ST_Code";
                    Name     = "ST_Name";
                    dt       = pm.SelProfession();
                }
                DataView dvTree = new DataView(dt);
                //过滤ParentID,得到当前的所有子节点
                dvTree.RowFilter = string.Format("{0} = '{1}'", ParentId, ParentID);
                foreach (DataRowView Row in dvTree)
                {
                    Node node = new Node();
                    if (pNode == null)
                    {
                        //添加根节点
                        node.Text = XYEEncoding.strHexDecode(Row[Name].ToString());
                        node.Tag  = XYEEncoding.strHexDecode(Row[Code].ToString());
                        ControlName.Nodes.Add(node);
                        AddTree(Row[Code].ToString(), node, table, ControlName);
                        //展开第一级节点
                        node.Expand();
                    }
                    else
                    {
                        //添加当前节点的子节点
                        node.Text = XYEEncoding.strHexDecode(Row[Name].ToString());
                        node.Tag  = XYEEncoding.strHexDecode(Row[Code].ToString());
                        pNode.Nodes.Add(node);
                        AddTree(Row[Code].ToString(), node, table, ControlName);     //再次递归
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载数据失败,请检查服务器连接并尝试刷新.错误:" + ex.Message);
            }
        }