How to reset nodes back to unbold in treeview in C# - c#

On Selecting a particular I want to highlight the treepath with nodes as bold one.
protected void Trv_SelectedNodeChanged(object sender, EventArgs e)
{
HighlightPath(Trv.SelectedNode);
}
private void HighlightPath(TreeNode node)
{
node.SelectAction = TreeNodeSelectAction.None;
node.Text = "<div style=font-weight:bold>" + node.Text + "</div>";
if (node.Parent != null)
HighlightPath(node.Parent);
}
But next time if I am slelecting another node its should mark previous nodes unbold and new path as bolded one. Please help on this.

You can solve this by stroring all highlighted nodes in a List<TreeNode> and before highlighting a new path, you un-highligt the items in the list.
// New list of currently highlighted nodes
protected List<TreeNode> highlightedNodes = new List<TreeNode>();
protected void Trv_SelectedNodeChanged(object sender, EventArgs e)
{
// Call the method to un-highligt first
UnHighlightNodes();
HighlightPath(Trv.SelectedNode);
}
private void UnHighlightNodes()
{
foreach(var node in highlightedNodes)
{
// TODO: code to un-highlight nodes
}
// Clear List of highlighted nodes
highlightedNodes.Clear();
}
private void HighlightPath(TreeNode node)
{
node.SelectAction = TreeNodeSelectAction.None;
node.Text = "<div style=font-weight:bold>" + node.Text + "</div>";
if (node.Parent != null)
HighlightPath(node.Parent);
// Store the node in the list of highlighted nodes
highlightedNodes.Add(node);
}
This could be optimized further to prevent un-highlighting and then immediatley highlighting again. Like this:
// New list of currently highlighted nodes
protected List<TreeNode> HighlightedNodes = new List<TreeNode>();
protected void Trv_SelectedNodeChanged(object sender, EventArgs e)
{
var nodesToHighlight = FindPathToRoot(Trv.SelectedNode).ToList();
// Un-highlight nodes (except those which should stay highlighted)
UnHighlightNodes(HighlightedNodes.Except(nodesToHighlight));
// Highlight nodes (except nodes already highlighted)
HighlightNodes(nodesToHighlight.Except(HighlightedNodes));
// Save the new list of highlighted nodes;
HighlightedNodes = nodesToHighlight;
}
private static IEnumerable<TreeNode> FindPathToRoot(TreeNode node)
{
do
{
yield return node;
node = node.Parent;
} while (node != null);
}
private void UnHighlightNodes(IEnumerable<TreeNode> nodes)
{
foreach (var node in nodes)
{
// TODO: code to un-highlight nodes
}
}
private void HighlightNodes(IEnumerable<TreeNode> nodes)
{
foreach (var node in nodes)
{
node.SelectAction = TreeNodeSelectAction.None;
node.Text = "<div style=font-weight:bold>" + node.Text + "</div>";
}
}

I think you need to bold selected node and its Parent node in a treeview asp.net page.
If its so you don't need to make it complex. Please try to put this in your page load event as shown .
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
foreach (TreeNode ParentNodes in TreeView1.Nodes)
{
ParentNodes.Text = ParentNodes.Text.Replace("<b>", "").Replace("</b>", "");
foreach (TreeNode ChildNodes in ParentNodes.ChildNodes)
{
ChildNodes.Text = ChildNodes.Text.Replace("<b>", "").Replace("</b>", "");
}
}
TreeNode node = TreeView1.SelectedNode;
node.Text = Server.HtmlDecode("<b>" + node.Text + "</b>");
node.Parent.Text = Server.HtmlDecode("<b>" + node.Parent.Text + "</b>");
}
}
Here TreeView1 is my treeview . You don't need to do anything in SelectedNodeChanged event for this.

Related

How to capture changed Select/UnSelected Checkbox Node in a TreeView

I have an asp Treeview which i made for my understanding.
I would like to capture all the check and uncheck checkboxes node user selects.
I have tried to use a button click event in which i check for a CheckNodes. But i would like to know how to get the UnSelected nodes before he click the Save Button.
As per code,
This is the format of my treeview
On Page Load only 3 nodes are checked.
What if the user unselects few node and presses the save button.
How do i know capture the information of checkbox selected/Unselect on postback.
User Action
Selects -- Head1Child1GrandChild1
Unselects -- Head1Child2 &&
Head1Child3
Expected Result : Need to capture checkbox
Selects -- Head1Child1 && Head1Child1GrandChild1 (Code in Save_Click)
AND Also need to capture
Unselects -- Head1Child2 &&
Head1Child3
The given treeview is just a prototype to understand the problem for me. Imagine if there is 1000 nodes and user selects some node and unselects the selected node . I just want to get those unselected and selected node only. Thats it.
Thank you for your time.
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" ShowCheckBoxes="All" Showlines="true" runat="server"></asp:TreeView>
<asp:Button ID="Button1" runat="server" OnClick="Save_Click" Text="Save" />
<br />
</div>
</form>
</body>
//CS Code
//TreeView on a PageLoad.
//It will create TreeView with all Checkbox checked
// If the user Unselects one check box how do i capture that in a event ?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
TreeNode tNode1 = new TreeNode();
tNode1.Text = "HeadNode1";
tNode1.Value = "HeadNode1";
TreeNode h1ChildNode1 = new TreeNode();
h1ChildNode1.Text = "Head1Child1";
tNode1.ChildNodes.Add(h1ChildNode1);
TreeNode h1GrandChild1 = new TreeNode();
h1GrandChild1.Text = "Head1Child1Grand1";
h1ChildNode1.ChildNodes.Add(h1GrandChild1);
TreeNode h1ChildNode2 = new TreeNode();
h1ChildNode2.Text = "Head1Child2";
tNode1.ChildNodes.Add(h1ChildNode2);
TreeNode h1ChildNode3 = new TreeNode();
h1ChildNode3.Text = "Head1Child3";
tNode1.ChildNodes.Add(h1ChildNode3);
TreeView1.Nodes.Add(tNode1);
TreeNode tNode2 = new TreeNode();
tNode2.Text = "HeadNode2";
tNode2.Value = "HeadNode2";
TreeView1.Nodes.Add(tNode2);
ServerSideChangeSelection(TreeView1, true);
}
protected TreeView ServerSideChangeSelection(TreeView t, bool check)
{
foreach (TreeNode tn in t.Nodes)
{
tn.Checked = false;
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode childNd in tn.ChildNodes)
{
childNd.Checked = check;
}
}
}
return t;
}
}
//Save Button Click for CheckNodes. Missing UnSelect Nodes ??
protected void Save_Click(object sender, EventArgs e)
{
List<string> checkNodes = new List<string>();
List<string> unCheckNodes = new List<string>();
foreach (var item in TreeView1.CheckedNodes)
{
checkNodes.Add(item.ToString());
}
}
I will make some changes to your code:
Created a property like below:
public List<TreeNode> Nodes
{
get
{
if (HttpContext.Current.Session["Nodes"] == null)
{
HttpContext.Current.Session["Nodes"] = new List<TreeNode>();
}
return HttpContext.Current.Session["Nodes"] as List<TreeNode>;
}
set
{
HttpContext.Current.Session["Nodes"] = value;
}
}
In your page_load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TreeNode tNode1 = new TreeNode();
tNode1.Text = "HeadNode1";
tNode1.Value = "HeadNode1";
TreeNode h1ChildNode1 = new TreeNode();
h1ChildNode1.Text = "Head1Child1";
tNode1.ChildNodes.Add(h1ChildNode1);
TreeNode h1GrandChild1 = new TreeNode();
h1GrandChild1.Text = "Head1Child1Grand1";
h1ChildNode1.ChildNodes.Add(h1GrandChild1);
TreeNode h1ChildNode2 = new TreeNode();
h1ChildNode2.Text = "Head1Child2";
tNode1.ChildNodes.Add(h1ChildNode2);
TreeNode h1ChildNode3 = new TreeNode();
h1ChildNode3.Text = "Head1Child3";
tNode1.ChildNodes.Add(h1ChildNode3);
TreeView1.Nodes.Add(tNode1);
TreeNode tNode2 = new TreeNode();
tNode2.Text = "HeadNode2";
tNode2.Value = "HeadNode2";
TreeView1.Nodes.Add(tNode2);
ServerSideChangeSelection(TreeView1, true);
List<TreeNode> nodes = new List<TreeNode>();
foreach (TreeNode node in TreeView1.Nodes)
{
nodes.Add(node);
if (node.ChildNodes.Count > 0)
{
foreach (TreeNode childNode in node.ChildNodes)
{
nodes.Add(childNode);
}
}
}
Nodes = nodes;
}
}
Save_Click:
protected void Save_Click(object sender, EventArgs e)
{
List<TreeNode> unCheckNodes = new List<TreeNode>();
GetCheckUncheckTreeNodes(TreeView1.Nodes, ref unCheckNodes);
}
private void GetCheckUncheckTreeNodes(TreeNodeCollection nodeCollection, ref List<TreeNode> unCheckNodes)
{
if (Nodes != null)
{
foreach (TreeNode node in nodeCollection)
{
if (!node.Checked && Nodes.Any(x => x.Text == node.Text && x.Checked != node.Checked))
unCheckNodes.Add(node);
if (node.ChildNodes.Count > 0)
GetCheckUncheckTreeNodes(node.ChildNodes, ref unCheckNodes);
}
}
}

WPF IsSelected prevent trigger when selecting child

I have a problem I've ran into and I'm not sure if it is possible to prevent it. I supose it is designed like this by default.
I have coded a treeview list to be filled by a XML and each of these node, when selected, are filling some textbox. Depending on their type, it will trigger a different function.
The problem is that when I select a child, it seems to trigger "IsSelecting" for all parents treeviewitem all the way to the top which in return trigger the associated function as well and I don't want that.
Any idea how to prevent this "Sort of reverse inheritance" for IsSelected?
Example (check with code below): selecting a "node" will trigger "Node_Selected", "Dialog_Selected", "Actor_Selected".
Thanks for your help.
Best regards,
Just for context:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
XmlDocument document = new XmlDocument();
document.Load("XML/ActorsDialogs.xml");
XmlNodeList actors = document.SelectNodes("/dialogs/actor");
foreach (XmlNode actor in actors)
{
TreeViewItem newActor = new TreeViewItem();
newActor.Header = actor.SelectSingleNode("actorname").InnerText;
newActor.Selected += new RoutedEventHandler(Actor_Selected);
XmlNodeList dialogs = actor.SelectNodes("dialog");
foreach (XmlNode dialog in dialogs)
{
TreeViewItem newdialog = new TreeViewItem();
newdialog.Header = "Dialog:" + dialog.SelectSingleNode("dialogID").InnerText;
newdialog.Selected += new RoutedEventHandler(Dialog_Selected);
BuildNodes(newdialog, dialog);
newActor.Items.Add(newdialog);
}
ActorsList.Items.Add(newActor);
}
}
private void BuildNodes(TreeViewItem treeNode, XmlNode parentElement)
{
foreach (XmlNode child in parentElement.ChildNodes)
{
if (child.Name == "node" || child.Name == "reply")
{
XmlElement childElement = child as XmlElement;
TreeViewItem childTreeNode = new TreeViewItem();
string ID = child.SelectSingleNode(child.Name + "ID").InnerText;
childTreeNode.Header = childElement.Name + ":" + ID;
switch (child.Name)
{
case "node":
childTreeNode.Selected += new RoutedEventHandler(Node_Selected);
break;
case "reply":
childTreeNode.Selected += new RoutedEventHandler(Reply_Selected);
break;
default:
break;
}
treeNode.Items.Add(childTreeNode);
BuildNodes(childTreeNode, childElement);
}
}
}
private void Actor_Selected(object sender, RoutedEventArgs e){}
private void Dialog_Selected(object sender, RoutedEventArgs e){}
private void Node_Selected(object sender, RoutedEventArgs e){}
private void Reply_Selected(object sender, RoutedEventArgs e){}
In the event handler you can set e.Handled = true. That will prevent the event from bubbling up the tree.
private void Node_Selected(object sender, RoutedEventArgs e)
{
e.Handled = true; //this will prevent the event from bubbling up to parents;
//Do the rest of the code here.
}
See https://msdn.microsoft.com/en-us/library/ms742806%28v=vs.110%29.aspx for more information on RoutedEvents. These include bubbling events which go up the tree and tunneling events which go down the tree.

c# - How to get checked node value from Treeview?

I have a Treeview with checkboxes when I click the parent node checkbox that child node will also be checked
I want to get the checked child node value
In the below code I got all the checked values even parent node value
But I don't want that parent node.
protected void btnAdd_Click(object sender, EventArgs e)
{
if (TreeView1.CheckedNodes.Count > 0)
{
foreach (TreeNode node in TreeView1.CheckedNodes)
{
string checkedValue = node.Text.ToString();
activityData = new ActivityData { ActivityName = checkedValue };
listActivity.Add(activityData);
Session["listActivity"] = listActivity;
}
}
}
In the above image I don't want to get HR Activity(Parent node)
Any ideas? Thanks in advance
Use the following code. The main parent node does not have a parent.
You only need to set the session variable at the end of the foreach
Keep in mind that accesing or assigning session variables means that a deserialization/serialization of the object is happening
Also I dont know if maybe you need to clear or initialize the listActivity before the foreach. You can do this either with
listActivity = new List<ActivityData>() ;
or
listActivity.Clear();
Here is the code
protected void btnAdd_Click(object sender, EventArgs e)
{
if (TreeView1.CheckedNodes.Count > 0)
{
foreach (TreeNode node in TreeView1.CheckedNodes)
{
//The main parent node does not have a parent
if(node.Parent != null)
{
string checkedValue = node.Text.ToString();
activityData = new ActivityData { ActivityName = checkedValue };
listActivity.Add(activityData);
}
}
//stablish the session variable only when the foreach has finished
Session["listActivity"] = listActivity;
}
}
There's a property of a treeview node called Level, so you could skip nodes with a level of 0, course that will break down if you nest a level deeper.
If you know which parent node it is, then iterate through it's children and test for checked.
protected void btnAdd_Click(object sender, EventArgs e)
{
if (TreeView1.CheckedNodes.Count > 0)
{
foreach (TreeNode node in TreeView1.CheckedNodes)
{
if (node.Level > 0)
{
string checkedValue = node.Text.ToString();
activityData = new ActivityData { ActivityName = checkedValue };
listActivity.Add(activityData);
Session["listActivity"] = listActivity;
}
}
}
}

ASP.net Tree-->SiteMap Skip the Root Node

I am using Ext.net TreePanel which does not have an option to skip the root node.
Basically, I have the following code
protected void Page_Load(object sender, EventArgs e)
{
SiteMapNode siteNode = SiteMap.RootNode;
Ext.Net.TreeNode root = this.CreateNode(siteNode);
root.Draggable = false;
root.Expanded = true;
TreePanel1.Root.Add(root);
}
private Ext.Net.TreeNode CreateNode(SiteMapNode siteMapNode)
{
Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();
treeNode.NodeID = siteMapNode.Key;
treeNode.Text = siteMapNode.Title;
treeNode.Qtip = siteMapNode.Description;
SiteMapNodeCollection children = siteMapNode.ChildNodes;
if (children != null && children.Count > 0)
{
foreach (SiteMapNode mapNode in siteMapNode.ChildNodes)
{
treeNode.Nodes.Add(this.CreateNode(mapNode));
}
}
return treeNode;
}
Could you please help to skip the parent node in the above example.
Thank you and regards.
A Root Node is required, although you can set RootVisible="false" on your <ext:TreePanel /> to hide it.
Hope this helps.

adding child nodes in treeview

I'm new to C# and don't have any programming experience. But I've finish a C# basics.
Now I would like to design a simple tree view by adding parent node and child node.
I would like to add a second child for the Second node, I'm quite stuck here and don't know what's next.
Any ideas?
Here is the code:
private void addParentNode_Click(object sender, EventArgs e)
{
string yourParentNode;
yourParentNode = textBox1.Text.Trim();
treeView2.Nodes.Add(yourParentNode);
}
private void addChildNode_Click(object sender, EventArgs e)
{
string yourChildNode;
yourChildNode = textBox1.Text.Trim();
treeView2.Nodes[0].Nodes.Add(yourChildNode);
}
Sorry I wasn't clear, I'm not sure if I really need this one here:
//treeView1.BeginUpdate();
//treeView1.Nodes.Clear();
What I'm trying to do, is to add Parent Nodes and child node. In my code, I can add several Parent Nodes, but if I want to add a child node, it only add in the first parent node.
I want that if I add a child node, I want to add it to the second parent or third parent.
In my code I only use one treeview here which names as treeview2
Here is the screenshot
this is how my final code looks like:
Before I put the else, I'm getting an error if I don't select anything. So I made it that way that if there is nothing selected it will add the "child node" to the "default node" or (parent1 node). It seems to work good. Thanks guys;-)
//This is for adding a parent node
private void addParentNode_Click(object sender, EventArgs e)
{
treeView2.BeginUpdate();
string yourParentNode;
yourParentNode = textBox1.Text.Trim();
treeView2.Nodes.Add(yourParentNode);
treeView2.EndUpdate();
}
//This is for adding child node
private void addChildNode_Click(object sender, EventArgs e)
{
if (treeView2.SelectedNode != null)
{
string yourChildNode;
yourChildNode = textBox1.Text.Trim();
treeView2.SelectedNode.Nodes.Add(yourChildNode);
treeView2.ExpandAll();
}
//This is for adding the child node to the default node(parent 1 node)
else
{
string yourChildNode;
yourChildNode = textBox1.Text.Trim();
treeView2.Nodes[0].Nodes.Add(yourChildNode);
}
Additional question: Are there any other ways on how the code be better? Because here, I declare the string "yourChildNode" twice. One in the if and other one in the else, are there any simplification?
It's not that bad, but you forgot to call treeView2.EndUpdate() in your addParentNode_Click() method.
You can also call treeView2.ExpandAll() at the end of your addChildNode_Click() method to see your child node directly.
private void addParentNode_Click(object sender, EventArgs e) {
treeView2.BeginUpdate();
//treeView2.Nodes.Clear();
string yourParentNode;
yourParentNode = textBox1.Text.Trim();
treeView2.Nodes.Add(yourParentNode);
treeView2.EndUpdate();
}
private void addChildNode_Click(object sender, EventArgs e) {
if (treeView2.SelectedNode != null) {
string yourChildNode;
yourChildNode = textBox1.Text.Trim();
treeView2.SelectedNode.Nodes.Add(yourChildNode);
treeView2.ExpandAll();
}
}
I don't know if it was a mistake or not but there was 2 TreeViews. I changed it to only 1 TreeView...
EDIT: Answer to the additional question:
You can declare the variable holding the child node name outside of the if clause:
private void addChildNode_Click(object sender, EventArgs e) {
var childNode = textBox1.Text.Trim();
if (!string.IsNullOrEmpty(childNode)) {
TreeNode parentNode = treeView2.SelectedNode ?? treeView2.Nodes[0];
if (parentNode != null) {
parentNode.Nodes.Add(childNode);
treeView2.ExpandAll();
}
}
}
Note: see http://www.yoda.arachsys.com/csharp/csharp2/nullable.html for info about the ?? operator.
May i add to Stormenet example some KISS (Keep It Simple & Stupid):
If you already have a treeView or just created an instance of it:
Let's populate with some data - Ex. One parent two child's :
treeView1.Nodes.Add("ParentKey","Parent Text");
treeView1.Nodes["ParentKey"].Nodes.Add("Child-1 Text");
treeView1.Nodes["ParentKey"].Nodes.Add("Child-2 Text");
Another Ex. two parent's first have two child's second one child:
treeView1.Nodes.Add("ParentKey1","Parent-1 Text");
treeView1.Nodes.Add("ParentKey2","Parent-2 Text");
treeView1.Nodes["ParentKey1"].Nodes.Add("Child-1 Text");
treeView1.Nodes["ParentKey1"].Nodes.Add("Child-2 Text");
treeView1.Nodes["ParentKey2"].Nodes.Add("Child-3 Text");
Take if farther - sub child of child 2:
treeView1.Nodes.Add("ParentKey1","Parent-1 Text");
treeView1.Nodes["ParentKey1"].Nodes.Add("Child-1 Text");
treeView1.Nodes["ParentKey1"].Nodes.Add("ChildKey2","Child-2 Text");
treeView1.Nodes["ParentKey1"].Nodes["ChildKey2"].Nodes.Add("Child-3 Text");
As you see you can have as many child's and parent's as you want and those can have sub child's of child's and so on....
Hope i help!
Example of adding child nodes:
private void AddExampleNodes()
{
TreeNode node;
node = treeView1.Nodes.Add("Master node");
node.Nodes.Add("Child node");
node.Nodes.Add("Child node 2");
node = treeView1.Nodes.Add("Master node 2");
node.Nodes.Add("mychild");
node.Nodes.Add("mychild");
}
It looks like you are only adding children to the first parent treeView2.Nodes[0].Nodes.Add(yourChildNode)
Depending on how you want it to behave, you need to be explicit about the parent node you wish to add the child to.
For Example, from your screenshot, if you wanted to add the child to the second node you would need:
treeView2.Nodes[1].Nodes.Add(yourChildNode)
If you want to add the children to the currently selected node, get the TreeView.SelectedNode and add the children to it.
Try TreeView to get an idea of how the class operates. Unfortunately the msdn documentation is pretty light on the code samples...
I'm missing a whole lot of safety checks here!
Something like (untested):
private void addChildNode_Click(object sender, EventArgs e) {
TreeNode ParentNode = treeView2.SelectedNode; // for ease of debugging!
if (ParentNode != null) {
ParentNode.Nodes.Add("Name Of Node");
treeView2.ExpandAll(); // so you can see what's been added
treeView2.Invalidate(); // requests a redraw
}
}
I needed to do something similar and came across the same issues. I used the AfterSelect event to make sure I wasn't getting the previously selected node.
It's actually really easy to reference the correct node to receive the new child node.
private void TreeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
//show dialogbox to let user name the new node
frmDialogInput f = new frmDialogInput();
f.ShowDialog();
//find the node that was selected
TreeNode myNode = TreeView1.SelectedNode;
//create the new node to add
TreeNode newNode = new TreeNode(f.EnteredText);
//add the new child to the selected node
myNode.Nodes.Add(newNode);
}
Guys use this code for adding nodes and childnodes for TreeView from C# code.
*
KISS (Keep It Simple & Stupid :)*
protected void Button1_Click(object sender, EventArgs e)
{
TreeNode a1 = new TreeNode("Apple");
TreeNode b1 = new TreeNode("Banana");
TreeNode a2 = new TreeNode("gree apple");
TreeView2.Nodes.Add(a1);
TreeView2.Nodes.Add(b1);
a1.ChildNodes.Add(a2);
}
void treeView(string [] LineString)
{
int line = LineString.Length;
string AssmMark = "";
string PartMark = "";
TreeNode aNode;
TreeNode pNode;
for ( int i=0 ; i<line ; i++){
string sLine = LineString[i];
if ( sLine.StartsWith("ASSEMBLY:") ){
sLine = sLine.Replace("ASSEMBLY:","");
string[] aData = sLine.Split(new char[] {','});
AssmMark = aData[0].Trim();
//TreeNode aNode;
//aNode = new TreeNode(AssmMark);
treeView1.Nodes.Add(AssmMark,AssmMark);
}
if( sLine.Trim().StartsWith("PART:") ){
sLine = sLine.Replace("PART:","");
string[] pData = sLine.Split(new char[] {','});
PartMark = pData[0].Trim();
pNode = new TreeNode(PartMark);
treeView1.Nodes[AssmMark].Nodes.Add(pNode);
}
}
You may do as follows to Populate treeView with parent and child node. And also with display and value member of parent and child nodes:
arrayRoot = taskData.GetRocordForRoot(); // iterate through database table
for (int j = 0; j <arrayRoot.length; j++) {
TreeNode root = new TreeNode(); // Creating new root node
root.Text = "displayString";
root.Tag = "valueString";
treeView1.Nodes.Add(root); //Adding the root node
arrayChild = taskData.GetRocordForChild();// iterate through database table
for (int i = 0; i < arrayChild.length; i++) {
TreeNode child = new TreeNode(); // creating child node
child.Text = "displayString"
child.Tag = "valueString";
root.Nodes.Add(child); // adding child node
}
}
SqlConnection con = new SqlConnection(#"Data Source=NIKOLAY;Initial Catalog=PlanZadanie;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
public void loadTree(TreeView tree)
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT [RAZDEL_ID],[NAME_RAZDEL] FROM [tbl_RAZDEL]";
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
tree.Nodes.Add(reader.GetString(1));
tree.Nodes[0].Nodes.Add("yourChildNode");
tree.ExpandAll();
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Ошибка с сообщением: " + ex.Message);
}
}
You can improve that code
private void Form1_Load(object sender, EventArgs e)
{
/*
D:\root\Project1\A\A.pdf
D:\root\Project1\B\t.pdf
D:\root\Project2\c.pdf
*/
List<string> n = new List<string>();
List<string> kn = new List<string>();
n = Directory.GetFiles(#"D:\root\", "*.*", SearchOption.AllDirectories).ToList();
kn = Directory.GetDirectories(#"D:\root\", "*.*", SearchOption.AllDirectories).ToList();
foreach (var item in kn)
{
treeView1.Nodes.Add(item.ToString());
}
for (int i = 0; i < treeView1.Nodes.Count; i++)
{
n = Directory.GetFiles(treeView1.Nodes[i].Text, "*.*", SearchOption.AllDirectories).ToList();
for (int zik = 0; zik < n.Count; zik++)
{
treeView1.Nodes[i].Nodes.Add(n[zik].ToString());
}
}
}
void tree()
{
int counter = 0;
int counter1 = 0;
int counter2 = 0;
treeView1.Nodes.Clear();
var node = db.ChartOfAccounts.
Include(c => c.chartOfAccounts)
.Where(c => c.parent_account == null).ToList();
foreach (var account in node)
{
treeView1.Nodes.Add(account.account_title);
var node1 = db.ChartOfAccounts
.Where(c => c.parent_account == account.c_id).ToList();
bool t1 = db.ChartOfAccounts.Any(c => c.parent_account == account.c_id);
if (!t1) { counter = counter + 1; }
else
{
foreach (var item in node1)
{ treeView1.Nodes[(counter)].Nodes.Add(item.account_title);
bool test1 = db.ChartOfAccounts
.Any(c => c.parent_account == item.c_id);
if (!test1) { counter1 = counter1 + 1; }
else {
var node2 = db.ChartOfAccounts
.Where(c => c.parent_account == item.c_id).ToList();
foreach (var item2 in node2)
{
treeView1.Nodes[counter].Nodes[(counter1)].Nodes.Add(item2.account_title);
bool test2 = db.ChartOfAccounts
.Any(c => c.parent_account == item2.c_id);
if (!test2) { counter2 = counter2 + 1; }
else
{
var node3 = db.ChartOfAccounts
.Where(c => c.parent_account == item2.c_id).ToList();
foreach (var item3 in node3)
{ treeView1.Nodes[counter].Nodes[counter1].Nodes[counter2].Nodes.Add(item3.account_title); }
}
}
}
}
counter = counter + 1;
}
}
}

Categories

Resources