一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net c实现省市区树形菜单代码

时间:2022-06-25 05:05:50 编辑:袖梨 来源:一聚教程网

asp教程.net c实现省市区树形菜单代码

///


  /// 创建dataset的方法
  ///

  /// treeview的根节点
  /// dataset数据集合
  /// 节点的父
  private void createtreeview(treenodecollection nodes, dataset ds, int pid)
  {
  //定义筛选条件
  string sql = string.format("parentid={0}", pid);

  //从dataset中筛选符合条件的行的集合
  datarow[] drr = ds.tables[0].select(sql);

  //遍历获得行的集合
  foreach (datarow dr in drr)
  {
  //创建treeview节点
  treenode tnode = new treenode();
  //添加到根节点中
  nodes.add(tnode);
  //设置显示的文本
  //tnode.imageurl = "images/foldericon1.gif";
  tnode.text = dr["purname"].tostring();
  tnode.value = dr["purid"].tostring();
  //设置显示的路径
  tnode.navigateurl = dr["pururl"].tostring();
  //递归调用方法
  createtreeview(tnode.childnodes, ds, convert.toint32(dr["purid"]));
  //从dataset中删除已经添加的行,提高访问效率
  ds.tables[0].rows.remove(dr);

  }
  }
 

///方法二
 

  private void treeviewbind()
    {
        string sql_tr = "select id,pid,name from tab";
        sqlconnection mysql教程conn = new sqlconnection(sqlconn);
        sqldataadapter mydataadapter = new sqldataadapter(sql_tr, mysqlconn);
        mysqlconn.open();
        mydataadapter.fill(ds_tr, "tree_show");
        dataview dv = ds_tr.tables[0].defaultview;
        dv.rowfilter = "pid=0";
        treeview1.showcheckboxes = treenodetypes.none; //不显示checkbox
        foreach (datarowview drv in dv)
        {
            treenode node = new treenode();
            node.text = drv["name"].tostring();
            node.value = drv["id"].tostring();
            node.expanded = false;
            treeview1.nodes.add(node);
            addnode(dv, node);
        }
    }

    /// 递归绑定子节点

    private void addnode(dataview dv, treenode node)
    {
        dv.rowfilter = "pid='" + node.value + "'";
        foreach (datarowview row in dv)
        {
            treenode replynode = new treenode();
            replynode.text = row["name"].tostring();
            replynode.value = row["id"].tostring();
            replynode.expanded = false;
            node.childnodes.add(replynode);
            addnode(dv, replynode);
        }
    }
 
 //方法三
 
/// treeview递归绑定数据
///先绑定province根据pif查询city
 private void inittree(treenodecollection nds,string parentid)
 {
 dataview dv=new dataview();
 treenode tmpnd;
 string intid;
 dv.table=ds.tables["tree"];
 dv.rowfilter="parentid='" + parentid + "'" ;
 foreach(datarowview drv in dv)
 {
 tmpnd=new treenode();
 tmpnd.tag=drv["nodeid"].tostring();
 tmpnd.text=drv["nodename"].tostring();
 nds.add(tmpnd);
 intid=drv["parentid"].tostring();
 inittree(tmpnd.nodes,tmpnd.tag.tostring());
 }

热门栏目