当前位置: 移动技术网 > IT编程>开发语言>c# > Devexpress treelist 简介

Devexpress treelist 简介

2019年07月18日  | 移动技术网IT编程  | 我要评论

节点折叠 this.treelist1.collapseall();

  一、简介

二、属性列表

1、optionsselection:

  enableappearanceforcusedcell:选中的cell的appearance设置是否可用。默认为true;

  enableappearanceforcusedrow:选中的node的appearance设置是否可用。默认为true

  invertselection:设置选中风格是只应用于选中的cell,还是应用于除选中的cell之外的所有cell。默认为false,即后者; 

  multiselect:是否可以选择多个node。默认为false。

2、optionsview:

   autocalcpreviewlinecount:是否自动计算预览节段的高度。默认为true;

   autowidth:是否允许列自动调整宽度;默认为true;

   enableappearanceevenrow:生成偶数node时,是采用由

  treelistappearancecollection.evenrow属性提供的appearance

    设置,还是采用由treelistappearancecollection.row提供的

   appearance设置。默认为false,即后者;

   enableappearanceoddrow:生成奇数node时,是采用由

  treelistappearancecollection.oddrow属性提供的appearance

    设置,还是采用由treelistappearancecollection.row提供的

   appearance设置。默认为false,即后者;

   showbuttons:是否显示展开与收缩按钮。默认为true;

   showcloumns:是否显示列标题。默认为true;

   showfocusedframe:在获得焦点的cell上,是否显示焦点框架。默认为true;

   showhorzlines:是否显示水平线。默认为true;

   showindentasrowstyle:是否用相应node的appearance设置来生成tree的缩进。默认为false

   showindicator:是否显示node的指示符面板。默认为true;

  showpreview:是否显示预览节段。默认为false;

  showroot:是否在根node间显示连接线。默认为true;

   showrowfootersummary:是否显示分组脚注。默认为false;

  showsummaryfooter:是否显示摘要脚注。默认为false;

  showvertlines:是否显示垂直线。默认为true;

3、selectimagelist:选中node时,显示图片的列表;

4、stateimagelist:指明node状态的图片的列表;

三、事件

四、使用

1、 如何隐藏treelist的列头

   设置treelistr的optionsview的showcolumns属性为:false

2、 如何默认展开所有的节点expandall()

   tlvwells.expandall();

    以及treenode.expand   =   false  

    或者你可以控制展开的层数 reeview1.expandlevel=10; 展开10层

  this.treelist.nodes[0].expandall();// 第一层下的所有接点展开

3、 如何让treelist的每个结点高亮显示?

代码如下:

private void treelist1_customdrawnodecell(object sender, devexpress.xtratreelist.customdrawnodecelleventargs e)
    {
      treelist node = sender as treelist;
      if (e.node == node.focusednode)
      {
        e.graphics.fillrectangle(systembrushes.window, e.bounds);
        rectangle r = new rectangle(e.editviewinfo.contentrect.left, e.editviewinfo.contentrect.top,
        convert.toint32(e.graphics.measurestring(e.celltext,  treelist1.font).width + 1), convert.toint32(e.graphics.measurestring(e.celltext,treelist1.font).height));
        e.graphics.fillrectangle(systembrushes.highlight, r);
        e.graphics.drawstring(e.celltext, treelist1.font, systembrushes.highlighttext, r);
        e.handled = true;
      }
    }

4、 数据绑定最基本的两个属性:keyfieldname和parentfieldname。

(这两个属性一设置就基本上可以实现分级了)可以通过代码的编写实现,也可以直接在属性里面直接实现。这种数据库设计是比较常见的,一般数据满足树形关系就可以这样设计。绑定数据时,只需指定datasource为对应 的datatable,指定keyfieldname为表主键字段,parentfieldname为表指向主键的外键字段名。

 private void binddata()
      {
      this.tloffice.datasource = dtoffice;
      tloffice.keyfieldname = "officeid";
      //tloffice.datamember = "officename";
      tloffice.columns["officename"].caption = "局名称";
      tloffice.parentfieldname = "parentofficeid";
    }

5、 选择某一节点时,该节点的子节点全部选择  取消某一节点时,该节点的子节点全部取消选择

      哪个节点引起行为的?节点是选中还是取消选中?由此确定方法的两个参数:treelistnode和checkstate。遍历该节点及其子孙,并将其选中状态设置为该节点的状态即可。

    /// 选择某一节点时,该节点的子节点全部选择 取消某一节点时,该节点的子节点全部取消选择
    /// <param name="node"></param>
    /// <param name="state"></param>
    private void setcheckedchildnodes(treelistnode node, checkstate check)
    {
      for (int i = 0; i < node.nodes.count; i++)
      {
        node.nodes[i].checkstate = check;
        setcheckedchildnodes(node.nodes[i], check);
      }
  } 

上两步写好了,别忘了上面的两个方法在treelist_afterchecknode里面触发:

private void tloffice_afterchecknode(object sender, devexpress.xtratreelist.nodeeventargs e) 
{ 
  setcheckedchildnodes(e.node, e.node.checkstate); 
  setcheckedparentnodes(e.node, e.node.checkstate); 
}

6、 某节点的子节点全部选择时,该节点选择;某节点的子节点未全部选择时,该节点不选择     

 /// 某节点的子节点全部选择时,该节点选择  某节点的子节点未全部选择时,该节点不选择
    /// <param name="node"></param>
    /// <param name="check"></param>
    private void setcheckedparentnodes(treelistnode node, checkstate check)
    {
      if (node.parentnode != null)
      {
        checkstate parentcheckstate = node.parentnode.checkstate;
        checkstate nodecheckstate;
        for (int i = 0; i < node.parentnode.nodes.count; i++)
        {
          nodecheckstate = (checkstate)node.parentnode.nodes[i].checkstate;
          if (!check.equals(nodecheckstate))//只要任意一个与其选中状态不一样即父节点状态不全选
          {
            parentcheckstate = checkstate.unchecked;
            break;
          }
          parentcheckstate = check;//否则(该节点的兄弟节点选中状态都相同),则父节点选中状态为该节点的选中状态
        }
        node.parentnode.checkstate = parentcheckstate;
        setcheckedparentnodes(node.parentnode, check);//遍历上级节点
      }
    }

 上两步写好了,别忘了上面的两个方法在treelist_afterchecknode里面触发: 

private void tloffice_afterchecknode(object sender, devexpress.xtratreelist.nodeeventargs e)
    {
      setcheckedchildnodes(e.node, e.node.checkstate);
      setcheckedparentnodes(e.node, e.node.checkstate);
    }

7、 获取选中的复选框数据列表

 private list<int> lstcheckedofficeid = new list<int>();//选择局id集合
    /// 获取选择状态的数据主键id集合
    /// <param name="parentnode">父级节点</param>
    private void getcheckedofficeid(treelistnode parentnode)
    {
      if (parentnode.nodes.count == 0)
      {
        return;//递归终止
      }
      foreach (treelistnode node in parentnode.nodes)
      {
        if (node.checkstate == checkstate.checked)
        {
          datarowview drv = tloffice.getdatarecordbynode(node) as datarowview;//关键代码,就是不知道是这样获取数据而纠结了很久(鬼知道可以转换为datarowview啊)
          if (drv != null)
          {
            int officeid = (int)drv["officeid"];
            lstcheckedofficeid.add(officeid);
          } 
        }
        getcheckedofficeid(node);
      }
    }

  下面测试获取主键列表:

 private void btncheck_click(object sender, eventargs e)
    {
      this.lstcheckedofficeid.clear();
      if (tloffice.nodes.count > 0)
      {
        foreach (treelistnode root in tloffice.nodes)
        {
          getcheckedofficeid(root);
        }
      }
      string idstr = string.empty;
      foreach (int id in lstcheckedofficeid)
      {
        idstr += id + " ";
      }
      messagebox.show(idstr);
    }

五、注意事项

1、从数据库中读取数据

方法一:直接点击控件

 这种方法连接后系统会自动生成一行代码:

 this. 数据库表名tableadapter.fill(this.datadataset4.数据库表名);

 这种方法生成后不会像写代码那样连接后就会把第一行默认为根节点。而且一旦你要把可执行文件拿走就不可用了。因为你在选择数据库的时候选的是绝对路径。所以最好用下面的方法。

方法二:

用代码连接数据库(写的代码方法很多)       

 string connectionstring = "provider=microsoft.jet.oledb.4.0;data source=";
      connectionstring += @"d:\data.mdb";//这用的是绝对路径

应该用相对路径。

(string connectionstring = @"provider=microsoft.jet.oledb.4.0;data source=";
      connectionstring += application.startuppath + @"\data.mdb";
 )
      system.data.oledb.oledbconnection con = new system.data.oledb.oledbconnection(connectionstring);
      con.open();
      system.data.oledb.oledbcommand cmd = new system.data.oledb.oledbcommand("select * from fs_staff", con);
      system.data.oledb.oledbdatareader reader = cmd.executereader();
      datatable table = new datatable();
      table.load(reader);
      gridcontrol2.datasource = table;

好了,以上内容是小编给大家简单介绍的devexpress treelist 知识,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网