在TreeList中拖放后保存更改

时间:2018-02-24 16:41:17

标签: winforms drag-and-drop devexpress treelist

拖拉机在Treelist中有效,但我不知道如何保存它们,因此当我关闭/打开我的项目时,它会恢复为Treelist中的旧值。例如,我向上或向下移动行,但当我再次打开项目时,将恢复旧位置

  private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
    {
        treeList1.OptionsDragAndDrop.DragNodesMode = checkBox1.Checked ? DragNodesMode.Single : DragNodesMode.None;
    }
    //</checkBox1>

    private DevExpress.XtraTreeList.Nodes.TreeListNode GetDragNode(IDataObject data)
    {
        return data.GetData(typeof(DevExpress.XtraTreeList.Nodes.TreeListNode)) as DevExpress.XtraTreeList.Nodes.TreeListNode;
    }

    //<listBox1>
    private string GetStringByNode(DevExpress.XtraTreeList.Nodes.TreeListNode node)
    {
        string ret = "";
        for (int i = 0; i < treeList1.Columns.Count; i++)
            ret += node.GetDisplayText(i) + (i < treeList1.Columns.Count - 1 ? "; " : ".");
        return ret;
    }

    private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
    {
        if (GetDragNode(e.Data) != null)
            e.Effect = DragDropEffects.Copy;
    }

    private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
        DevExpress.XtraEditors.ListBoxControl lb = sender as DevExpress.XtraEditors.ListBoxControl;
        DevExpress.XtraTreeList.Nodes.TreeListNode node = GetDragNode(e.Data);
        if (node != null)
        {
            string dragString = GetStringByNode(node);
            int ind = lb.IndexFromPoint(lb.PointToClient(new Point(e.X, e.Y)));
            if (ind == -1)
                lb.Items.Add(dragString);
            else
                lb.Items.Insert(ind, dragString);
        }
    }

1 个答案:

答案 0 :(得分:0)

TreeList保存由于将节点移动到另一个父节点而导致的更改,因为此节点的 ParentID 字段已相应更新。但是,如果在同一子节点集合中移动节点,则这些更改将丢失。这就是为什么要完成此任务,您需要创建一个额外的数据源列来存储每个节点的当前索引。 How to change a TreeList node position along with a corresponding record's position in the database示例说明了这种方法。看看吧。