TREELIST 与数据库绑定

本文展示了如何将TREELIST控件与数据库进行绑定,通过C#代码实现与SQL Server数据库中Customers和Orders表的交互,创建父子节点结构,以便于展示客户及其对应的订单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
        }
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值