The data source is an Access database;
Connecting to the database - through the code;
After dragging the "node" into the TreeList, the sequence number of the "node" is updated in the "sort" field.
Problem: for a node that is being dragged, the "sort" field in the database is not updated.
For the remaining nodes that are in the same parent, the field update works correctly.
Update
Statistics
The initial state. The node "Node_1" is located in "Parent_1".
"Parent_1"-> "Node_1" -> "Parent_2".
Dragging the node "Node_1" from the parent "Parent_1" to "Parent_2". The numbering of the "sorting" field for the nodes in the database table is correct."Parent_2". "Node_2" <-> "Node_1" <-> "Node_3".
Dragging node "Node_1" between child nodes "Parent_2".
For a node that is being dragged, the "sort" field in the database is not updated.
For the remaining nodes that are in the same parent, the field update works correctly."Parent_1" <- "Node_1" <- "Parent_2".
Dragging node "Node_1" from parent "Parent_2" to "Parent_1" - in the lineadapter.Update (dt);
error: "Concurrency violation: UpdateCommand has affected 0 of the expected 1 entries."
How to make the program correctly update the “sorting” field while dragging a node in the treelist and in the database?
//
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
namespace rsh
{
public partial class Form1 : Form
{
DataTable dt;
OleDbDataAdapter adapter;
OleDbCommandBuilder AccessCommandBuilder;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
connect();
//
UpdateNodesPositions(treeList1.Nodes);
treeList1.ExpandAll();
}
#region *** DB ***
public void connect()
{
string catBD = @"c:\test\visualStudio\csharp\01\01.accdb";
string conBD = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", catBD);
OleDbConnection connection = new OleDbConnection(conBD);
connection.Open();
string query1 = "SELECT * FROM TableTreeView_12";
OleDbCommand cmd1 = new OleDbCommand(query1, connection);
dt = new DataTable();
adapter = new OleDbDataAdapter(cmd1);
AccessCommandBuilder = new OleDbCommandBuilder(adapter);
adapter.Fill(dt);
treeList1.KeyFieldName = "ID";
treeList1.ParentFieldName = "PrID";
treeList1.DataSource = dt;
}
public void Save()
{
adapter.Update(dt);
}
#endregion *** DB ***
private void treeList1_AfterDragNode(object sender, DevExpress.XtraTreeList.AfterDragNodeEventArgs e)
{
SaveNewRecordPosition(e);
}
private void SaveNewRecordPosition(NodeEventArgs e)
{
var nodes = e.Node.ParentNode == null
? e.Node.TreeList.Nodes
: e.Node.ParentNode.Nodes;
richTextBox1.Clear();
for (var i = 0; i < nodes.Count; i++)
{
nodes[i].SetValue(0, i);
string id = nodes[i].GetValue(0).ToString();
string s = nodes[i].GetValue(1).ToString();
richTextBox1.Text += id + " -//- " + s + "\r\n";
}
Save();
}
private void UpdateNodesPositions(TreeListNodes nodes)
{
var ns = new List<TreeListNode>();
foreach (TreeListNode n in nodes)
{
ns.Add(n);
}
foreach (TreeListNode n in ns)
{
UpdateNodesPositions(n.Nodes);
n.TreeList.SetNodeIndex(n, Convert.ToInt32(n.GetValue("sorting")));
}
}
}
}