zoukankan      html  css  js  c++  java
  • c# TreeView

    树形绑定数据

    SQL语句:提出需要绑定的父节点和子节点的数据语句,一般包括要显示的数据和主键ID。

    如:

    (父节点)

     if @type='dep'
     begin
     select departmentName, departmentId  from wb_department
     end
     (子节点)
     if @type='pro'
     begin
     select professionId,professionName,departmentId  from wb_profession
     end

     

     

    c#代码

    public DataSet ds = new DataSet();

     protected void Page_Load(object sender, EventArgs e)  

       {        

       ds.Tables.Add(Class1.dep());        

      ds.Tables[0].TableName = "depTable";        

      ds.Tables.Add(Class1.pro());        

      ds.Tables[1].TableName = "proTable";       

        ds.Relations.Add("dep_pro", ds.Tables[0].Columns["departmentId"], ds.Tables[1].Columns["departmentId"]);

     

         foreach (DataRow FatherRow in ds.Tables[0].Rows)     

        {           

        TreeNode FatherNode = new TreeNode((string)FatherRow["departmentName"]);            

       //asp.net中c#语句是这样: FatherNode.Target = FatherRow["departmentId"].ToString();

        FatherNode.Tag = FatherRow["departmentId"].ToString();           

        TreeView1.Nodes.Add(FatherNode);

            foreach (DataRow ChildrenRow in FatherRow.GetChildRows("dep_pro"))            

       {                

        TreeNode ChildrenNode = new TreeNode((string)ChildrenRow["professionName"]);                

        //ChildrenNode.Target = ChildrenRow["professionId"].ToString();

            ChildrenNode.Tag= ChildrenRow["professionId"].ToString();                

        //FatherNode.ChildNodes.Add(ChildrenNode);            

        FatherNode.Nodes.Add(ChildrenNode);  

       }

        }    

    }

    动态添加父节点

    TreeNode node = new TreeNode();            

    node.Text = textBox1.Text.ToString();            

    this.treeView1.Nodes.Add(node);

    TreeNode node1 = new TreeNode();            

    this.treeView1.Nodes[0].Nodes.Add(node1);

    TreeView-----节点单击事件:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                Form form2 = new Form();
                form2.ShowDialog();
            }

     


     

  • 相关阅读:
    java split
    百度知道
    2014年10月27日
    2014年10月27日
    mybatis批量update,返回行数为-1
    mybatis批量插入:oracle和mysql的区别
    oracle数据库,mybatis批量insert,缺失values字段
    java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
    C++——运算符重载(上)
    C++——友元
  • 原文地址:https://www.cnblogs.com/dieaz5/p/2585234.html
Copyright © 2011-2022 走看看