TREELIST 与数据库绑定

来源:互联网 发布:淘宝app在线人工客服 编辑:程序博客网 时间:2024/05/18 22:50

PS:结合KARLI的数据库代码和网络一位兄弟的TREELIST代码,写了如下的TREELIST 与数据库绑定的代码,希望对各位有点小帮助。      

private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection thisConn = new SqlConnection(@"server=sun;database=Northwind;user=sa;password=0810");
            SqlDataAdapter thisDA = new SqlDataAdapter("SELECT CustomerID,CompanyName FROM Customers", thisConn);
            SqlCommandBuilder thisCB = new SqlCommandBuilder(thisDA);

            DataSet thisDS = new DataSet();

            SqlDataAdapter custAD = new SqlDataAdapter("SELECT * FROM Customers", thisConn);
            custAD.Fill(thisDS, "Customers");
            SqlDataAdapter orderAD = new SqlDataAdapter("SELECT * FROM Orders", thisConn);
            orderAD.Fill(thisDS, "Orders");

           DataRelation custOrderRel = thisDS.Relations.Add("CustOrders", thisDS.Tables["Customers"].Columns["CustomerID"], thisDS.Tables["Orders"].Columns["CustomerID"]);

           treeView1.Nodes.Clear();
           TreeNode tableTree = new TreeNode("客户");
           treeView1.Nodes.Add(tableTree);

          
            foreach (DataRow custRow in thisDS.Tables["Customers"].Rows)
           {
               TreeNode newNode = new TreeNode(custRow["CompanyName"].ToString());
               tableTree.Nodes.Add(newNode);
               int depth = newNode.Index;
                foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel ))
                {
                    TreeNode newNode1=new TreeNode(orderRow ["OrderID"].ToString());
                    newNode.Nodes.Add(newNode1);                  
                }

           }
           treeView1.ExpandAll();
           thisConn.Close();
        }
 

原创粉丝点击