zoukankan      html  css  js  c++  java
  • DevExpress控件之XtraTreeList

     

        DevExpress控件,最近的项目里有用到·功能确实强大,一个控件抵的上以前vs自带控件的几个,算得上是复合控件的。今天来记下XtraTreeList 控件使用,备忘···

    1.treeList1绑定数据

    public void InitDate()
            {
                ds = SqlData.QueryDataSet("select deptid,name,code,rootid from dbo.bm_dept");

                treeList1.OptionsBehavior.PopulateServiceColumns = true;
                //this.treeList1.PopulateColumns();
                this.treeList1.ParentFieldName = "rootid";
                this.treeList1.KeyFieldName = "deptid";

                this.treeList1.DataSource = ds.Tables[0];
            }

    其中的 this.treeList1.ParentFieldName = "rootid";
                this.treeList1.KeyFieldName = "deptid";很重要 ParentFieldName 设置的字段是从属于KeyFieldName设置的字段的。如果A数据ParentFieldName 字段等于B数据的KeyFieldName字段,那A是B的子节点,设置成0 那A是根节点·

    所以添加新数据时,要注意KeyFieldName字段的选择,确立从属关系··

    2.修改时 具备XtraGridControl 列修改的功能

    private void treeList1_CellValueChanged(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
            {
                try
                {
                    string colKey = e.Column.FieldName;
                    string sqlStr = "";
                    switch (colKey)
                    {
                            //select deptid,name,code,rootid from bm_dept
                        case "deptid":
                            sqlStr = string.Format("update bm_dept set {0}='{1}' where deptid='{2}'", colKey, e.Value, treeList1.FocusedNode.GetValue("deptid").ToString());
                            break;
                        case "name":
                            sqlStr = string.Format("update bm_dept set {0}='{1}' where deptid='{2}'", colKey, e.Value, treeList1.FocusedNode.GetValue("deptid").ToString());
                            break;
                        default:
                            break;
                    }
                    if (!sqlStr.Trim().Equals(""))
                        SqlData.ExecuteSql(sqlStr);
                    InitDate();
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message.ToString());
                }
            }

    3.删除功能 注意:treeList1.FocusedNode.GetValue("deptid").ToString() 获取行的列值

    private void sBtnDel_Click(object sender, EventArgs e)
            {
                //if(treeList1.FocusedNode != null)
                //    treeList1.DeleteNode(treeList1.FocusedNode);
                int deid = int.Parse(treeList1.FocusedNode.GetValue("deptid").ToString());
                SqlDataReader dr = SqlData.QueryDataReader("select rootid from bm_dept");
                while (dr.Read())
                {
                    int rtid = int.Parse(dr["rootid"].ToString());
                    if ( rtid== deid)
                    {
                        MessageBox.Show("根节点不能删除!请先删除子节点。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }

                if (treeList1.FocusedNode != null)
                {
                    if (MessageBox.Show("确认删除嘛?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
                    {
                        try
                        {
                            int i= SqlData.ExecuteSql("delete from bm_dept where deptid='" + treeList1.FocusedNode.GetValue("deptid").ToString() + "'");
                            if (i > 0)
                            {
                                InitDate();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
                else
                { MessageBox.Show("请选择行", "系统提示"); }

            }

    4.TreeList 节点拖放

    string mubiao tuo ;//全局变量

    private void treeList1_DragDrop(object sender, DragEventArgs e)
            {
                DevExpress.XtraTreeList.TreeListHitInfo hi = treeList1.CalcHitInfo(treeList1.PointToClient(new Point(e.X, e.Y)));
                mubiao = hi.Node.GetValue("deptid").ToString();

                if (index == 0)
                {
                    //MessageBox.Show("放到当前节点的下级");
                    string sql = string.Format("update bm_dept set rootid='{0}' where deptid='{1}'", mubiao, tuo);
                    SqlData.ExecuteSql(sql);

                }
                else
                {
                    //MessageBox.Show("放到当前节点同级");
                    SqlDataReader dr = SqlData.QueryDataReader("select rootid from bm_dept where deptid='" + mubiao + "'");
                    dr.Read();
                    string roots=dr["rootid"].ToString();
                    string sql = string.Format("update bm_dept set rootid='{0}' where deptid='{1}'", roots, tuo);
                    SqlData.ExecuteSql(sql);
                }

              
                mubiao = "";
            }

            private void treeList1_DragEnter(object sender, DragEventArgs e)
            {
                tuo = treeList1.FocusedNode.GetValue("deptid").ToString();
            }

            private void treeList1_CalcNodeDragImageIndex(object sender, DevExpress.XtraTreeList.CalcNodeDragImageIndexEventArgs e)
            {
                index = e.ImageIndex;
            }

  • 相关阅读:
    JAVA导出EXCEL表格
    解决springboot配置@ControllerAdvice不能捕获 NoHandlerFoundException问题
    Mysql 查看定时器 打开定时器 设置定时器时间
    IDEA @Autowired 出现红色下划线 报红
    IntelliJ IDEA报warn class is never used
    UML类图符号 各种关系说明以及举例
    提升单元测试体验的利器--Mockito使用总结
    maven2中snapshot快照库和release发布库的应用
    Maven最佳实践-distributionManagement
    访问GitLab的PostgreSQL数据库
  • 原文地址:https://www.cnblogs.com/bingyun84/p/1646280.html
Copyright © 2011-2022 走看看