Welcome

DB for Tree View

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; using TreeViewLib; namespace FileSystem { public partial class frmTreeViewOp : Form { TreeViewOp _objTreeView = null; DataSet _ds = new DataSet(); public frmTreeViewOp() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { //UpdateNodes(); } catch (Exception ex) { throw ex; } } private void button3_Click(object sender, EventArgs e) { try { //folderBrowserDlg.SelectedPath = @"D:\Test"; DialogResult res = folderBrowserDlg.ShowDialog(); if (res == DialogResult.OK) { textBox1.Text = folderBrowserDlg.SelectedPath; DirectoryTree(Tv, textBox1.Text); } } catch (Exception ex) { throw ex; } } private void DirectoryTree(TreeView p_treeView, string path) { //p_treeView.Nodes.Clear(); //var rootDirectoryInfo = new DirectoryInfo(path); //p_treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo)); } //private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo) //{ //var directoryNode = new TreeNode(directoryInfo.Name); //foreach (var directory in directoryInfo.GetDirectories()) // directoryNode.Nodes.Add(CreateDirectoryNode(directory)); //foreach (var file in directoryInfo.GetFiles()) // directoryNode.Nodes.Add(new TreeNode(file.Name)); //return directoryNode; //} private void Form1_Load(object sender, EventArgs e) { try { fillTreeView(); } catch (Exception ex) { throw ex; } } private void fillTreeView() { try { _objTreeView = new TreeViewOp(); _ds =_objTreeView.GetData(); _ds.Relations.Add("NodeRelation", _ds.Tables[0].Columns["NodeId"], _ds.Tables[0].Columns["ParentNode"], false); Tv.Nodes.Clear(); foreach (DataRow _dr in _ds.Tables[0].Rows) { if (Convert.ToInt32(_dr["ParentNode"]) == 0) { TreeNode t = new TreeNode(_dr["NodeName"].ToString()); t.Tag = _dr["NodeId"].ToString(); Tv.Nodes.Add(t); RecursivelyPopulate(_dr, t); } } Tv.ExpandAll(); } catch (Exception ex) { throw ex; } } private void RecursivelyPopulate(DataRow _drw, TreeNode node) { foreach (DataRow childRow in _drw.GetChildRows("NodeRelation")) { TreeNode childNode = new TreeNode(childRow["NodeName"].ToString()); childNode.Tag = childRow["NodeID"].ToString(); node.Nodes.Add(childNode); RecursivelyPopulate(childRow, childNode); } } private void btnAdd_Click(object sender, EventArgs e) { try { int _intNewId = -1; _objTreeView = new TreeViewOp(); if (Tv.SelectedNode != null) { _objTreeView.AddTreeData(txtNode.Text, int.Parse(Tv.SelectedNode.Tag.ToString()), out _intNewId); TreeNode t = new TreeNode(); t.Text = txtNode.Text; t.Tag = _intNewId; Tv.SelectedNode.Nodes.Add(t); Tv.SelectedNode = t; } else { Tv.Nodes[0].Nodes.Add(textBox1.Text.Trim()); } } catch (Exception ex) { throw ex; } } private void button2_Click(object sender, EventArgs e) { try { if (Tv.SelectedNode != null) { _objTreeView = new TreeViewOp(); //Tv.SelectedNode.Nodes.Remove(Tv.SelectedNode); _objTreeView.DeleteTreeData(Convert.ToInt32(Tv.SelectedNode.Tag)); fillTreeView(); } } catch (Exception ex) { throw ex; } } private void DeleteNode(TreeNode tn) { TreeNode parent = tn.Parent; int nodeCount = tn.Nodes.Count; TreeNode[] nodes = new TreeNode[nodeCount]; if (tn.Nodes.Count != 0) for (int i = 0; i < nodeCount; i++) nodes[i] = tn.Nodes[i]; for (int i = 0; i < nodeCount; i++) DeleteNode(nodes[i]); tn.Remove(); } } }