wiki:dokuwiki:cs:treeviewのノードの状態をdbに保存する

TreeViewのNodeコレクションの状態をSQLiteに保存する

まず、保存するためのテーブルを設計します。
【SQLite】

  1. CREATE TABLE "TREEVIEW" (
  2. "ROWNAME" TEXT,
  3. "hasChild" INTEGER NOT NULL DEFAULT 0,
  4. "parentName" TEXT NOT NULL,
  5. "index" INTEGER NOT NULL,
  6. PRIMARY KEY("ROWNAME")
  7. );
  • ROWNAME TreeNodeの名前を保存する
  • hasChild そのNodeがNodesを持っていれば1
  • parentName 親ノードの名前を保存する
  • index Nodeを全て展開したときの見える順番にseq番号を振る

【C#】

  1. using System.Linq;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6.  
  7. namespace learning
  8. {
  9. public partial class Form1 : Form
  10. {
  11.  
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16.  
  17.  
  18.  
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. //https://dobon.net/vb/dotnet/control/tvdraganddrop.html
  22. //TreeView1へのドラッグを受け入れる
  23. treeView1.AllowDrop = true;
  24. //イベントハンドラを追加する
  25. treeView1.ItemDrag +=
  26. new ItemDragEventHandler(TreeView1_ItemDrag);
  27. treeView1.DragOver +=
  28. new DragEventHandler(TreeView1_DragOver);
  29. treeView1.DragDrop +=
  30. new DragEventHandler(TreeView1_DragDrop);
  31. }
  32.  
  33.  
  34. private void button2_Click(object sender, EventArgs e)
  35. {
  36. treeView1.Nodes.Clear();
  37.  
  38. var connectionString = "Data Source=learning.db";
  39. using (var cn = new SQLiteConnection(connectionString))
  40. {
  41. cn.Open();
  42. using (var cmd = new SQLiteCommand(cn))
  43. {
  44. using (var context = new DataContext(cn))
  45. {
  46.  
  47.  
  48. Dictionary<int, TreeNode> Dtree = new Dictionary<int, TreeNode>();
  49. TREEVIEW[] clone = context.GetTable<TREEVIEW>().ToArray().Clone() as TREEVIEW[];
  50.  
  51. foreach (var q in context.GetTable<TREEVIEW>().ToArray().OrderBy(y => y.index))
  52. {
  53. Dtree.Add(q.index, new TreeNode(q.ROWNAME));
  54. }
  55.  
  56. foreach (var q in context.GetTable<TREEVIEW>().ToArray().OrderByDescending(y => y.index))
  57. {
  58. var name = q.ROWNAME;
  59. var pare = q.parentName;
  60. if (pare != "ROOT")
  61. {
  62. var gg = clone.Single(x => x.ROWNAME == pare);
  63. Dtree[gg.index].Nodes.Insert(0,Dtree[q.index].Clone() as TreeNode);
  64. Dtree.Remove(q.index);
  65. }
  66. }
  67.  
  68. foreach(var q in context.GetTable<TREEVIEW>().ToArray().Where(y => y.parentName == "ROOT").OrderBy(z => z.index))
  69. {
  70. treeView1.Nodes.Add(Dtree[q.index]);
  71. }
  72. }
  73. }
  74.  
  75. }
  76.  
  77. treeView1.ExpandAll();
  78. }
  79.  
  80. private void button1_Click(object sender, EventArgs e)
  81. {
  82.  
  83. treeToDb(treeView1.Nodes);
  84.  
  85.  
  86. }
  87.  
  88. private void treeToDb(TreeNodeCollection tnc)
  89. {
  90. var connectionString = "Data Source=learning.db";
  91. int c = 0;
  92. using (var cn = new SQLiteConnection(connectionString))
  93. {
  94. cn.Open();
  95. using (var cmd = new SQLiteCommand(cn))
  96. {
  97. using (var context = new DataContext(cn))
  98. {
  99.  
  100. context.ExecuteCommand("DELETE FROM TREEVIEW");
  101. var views = context.GetTable<TREEVIEW>();
  102.  
  103. recursiveTree(views, context, tnc, ref c);
  104.  
  105. }
  106. }
  107.  
  108. }
  109. }
  110.  
  111. private void recursiveTree(Table<TREEVIEW> table, DataContext dc, TreeNodeCollection treeNodeCollection, ref int C)
  112. {
  113. foreach (TreeNode tnn in treeNodeCollection)
  114. {
  115.  
  116.  
  117. var Rowname = tnn.Text;
  118. var hAschild = tnn.GetNodeCount(true) > 0 ? 1 : 0;
  119. var lEvel = tnn.Level;
  120. var parent = tnn.Parent is null ? "ROOT" : tnn.Parent.Text;
  121. var kind = tnn.ImageIndex;
  122.  
  123. table.InsertOnSubmit(new TREEVIEW(Rowname, hAschild, parent,C));
  124. dc.SubmitChanges();
  125. C++;
  126.  
  127. if (hAschild == 1)
  128. {
  129. recursiveTree(table, dc, tnn.Nodes, ref C);
  130. }
  131. }
  132. }
  133. }
  134.  
  135. [Table(Name = "TREEVIEW")]
  136. public class TREEVIEW
  137. {
  138.  
  139. [Column(Name = "ROWNAME", CanBeNull = false, DbType = "NVARCHAR", IsPrimaryKey = true)]
  140. public string ROWNAME { get; set; }
  141.  
  142. [Column(Name = "hasChild", CanBeNull = false, DbType = "int", IsPrimaryKey = false)]
  143. public int hasChild { get; set; }
  144.  
  145. [Column(Name = "parentName", CanBeNull = false, DbType = "NVARCHAR", IsPrimaryKey = false)]
  146. public string parentName { get; set; }
  147.  
  148. [Column(Name = "index", CanBeNull = false, DbType = "int", IsPrimaryKey = false)]
  149. public int index { get; set; }
  150.  
  151. public TREEVIEW() { }
  152.  
  153. public TREEVIEW(string rowname, int HasChild, string ParentName,int Index)
  154. {
  155. ROWNAME = rowname;
  156. hasChild = HasChild;
  157. parentName = ParentName;
  158. index = Index;
  159.  
  160. }
  161.  
  162.  
  163.  
  164. }
  165. }

より、ドラッグ&ドロップでNodeを移動できるようにしておくと便利です。(上記コード内にはメソッド名のみ記載しています。)

コメントを入力. Wiki文法が有効です:
 
  • wiki/dokuwiki/cs/treeviewのノードの状態をdbに保存する.txt
  • 最終更新: 2024/11/04 00:47
  • by 127.0.0.1