Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Our application in C# winform.
I set nodes in treeview in usercontrol dynamically from access table. In this Parent node is department and child node is subdepartment through DeptID key. Now I want to access Subdeptid when i click childnode. Also I want to access Deptid when i click Parentnode. I tried and i got Node but didn't get its Deptid or subdeptid.
How could achieve this ?


here is code
C#
//on Load 
 BALDeaprtment objbaldepat=new BALDeaprtment();            
            DataSet ds = new DataSet();
            ds = objbaldepat.GetDepartmenttreeData(Globalclass.Activedatabase.ToString());
            foreach (DataRow dr in ds.Tables["Department"].Rows)
            {
                TreeNode tn = new TreeNode(dr["DeptName"].ToString());
                foreach (DataRow drChild in dr.GetChildRows("Dept_sub"))
                {
                    tn.Nodes.Add(drChild["SubDeptName"].ToString());
                }
                treeViewDept.Nodes.Add(tn);                
            }

///on afterselect code 

I got node but i cant get Deptid,Subdeptid how could i acheive this?...
Posted
Updated 13-Dec-13 1:44am
v4
Comments
CHill60 13-Dec-13 7:22am    
You say you have tried but you have not posted the code that you tried - use the Improve question link above so that we can see where your problem is
sp_suresh 13-Dec-13 7:39am    
please see code has given...
Maciej Los 13-Dec-13 7:32am    
Not many useful information... ;(
Please be more specific and provide more details (method to load data, algorith which you use to read DeptId and SubDeptId from treeview.
sp_suresh 13-Dec-13 7:39am    
please see code has given...
Maciej Los 13-Dec-13 7:46am    
I see that only names of Departments and Subdepartments are loaded... Or am i missed something?

keep DeptId and SubDeptId in Tag property of tree nodes
C#
foreach (DataRow dr in ds.Tables["Department"].Rows)
            {
                TreeNode tn = new TreeNode(dr["DeptName"].ToString());
                tn.Tag = dr["DeptId"];
                foreach (DataRow drChild in dr.GetChildRows("Dept_sub"))
                {
                    TreeNode tnChild = tn.Nodes.Add(drChild["SubDeptName"].ToString());
                    tnChild.Tag = drChild["SubDeptId"];
                }
                treeViewDept.Nodes.Add(tn);
            }


then if you know treeViewDept.SelectedNode, you can get id:
C#
int id = (int)treeViewDept.SelectedNode.Tag;
 
Share this answer
 
v2
Comments
Maciej Los 13-Dec-13 8:06am    
It's some idea, but not perfect, a4!
Please, see my answer.
sp_suresh 13-Dec-13 8:15am    
Thank you very much Alexander
Solution 1 by Alexander Sharykin is good, but it's semi-professional solution.

I would suggest you to read it: How to create a Key property for a TreeView node in Visual C#[^]. It's good to know how to extend class ;)

For further information, please see:
Classes (C#)[^]
Inheritance (C#)[^]
Polymorphism (C#)[^]
Inheritance and Derived Classes[^]
Advanced C# Techniques[^]
 
Share this answer
 
v2
Comments
sp_suresh 13-Dec-13 8:17am    
thanks Maceiej
I will try also this ....
Maciej Los 13-Dec-13 8:20am    
You're very welcome ;)
Alexander Sharykin 13-Dec-13 8:30am    
key should be unique for each node, and in case of department hierarchy this approach works.
and what if someone want to display departments - employees relationship?
it may happen that a department and an employee has the same Id
Maciej Los 13-Dec-13 8:41am    
TreeView is flexible class, as many others. It's just an example. Do not focus on Key. Keep focus on class inheritance and polymorphism, which means you can extend class adding many different properties ;)
I'd like to show you how easy it is to extend the TreeNode Class so that each Node in your TreeView can contain instantly accessible information of your choice. You don't need to sub-class the TreeView.

I'll start by defining a Class named 'Information:
C#
public class Information
{
    public string ID { get; private set; }
    public string DeptName { get; private set; }
    public string SubDeptName { get; private set; }

    public Information(string id, string deptname, string subdeptname)
    {
        ID = id;
        DeptName = deptname;
        SubDeptName = subdeptname;
    }
}
Why bother with a class, when we could just create a class that inherits from TreeNode in which you'd have fields ? The choice is based on the expectation that many nodes will share common Department Names and SubDeptNames, and using a Class means we stick a pointer to the same instance of 'Information into many Nodes ... hopefully that avoids a lot of duplicate strings.

The custom TreeNode Class:
C#
public class TreeNodeEx : TreeNode
{
    public Information NodeInformation { get; set; }

    public TreeNodeEx(Information info)
    {
        NodeInformation = info;
        this.Text = info.ID;
    }
}
Let's look how we might create some instances of the custom TreeNode class:
// somewhere in a method ...

private string id, dept, subdept;

id = "Marketing";
dept = "Advertising";
subdept = "Internet Ads";

// create an instance of the 'Information class
Information info = new Information(id, dept, subdept);

// create 10 custom TreeNodes
// add them to the TreeView
for (int i = 0; i < 10; i++)
{
    TreeNodeEx newNode = new TreeNodeEx(info);
    treeView1.Nodes.Add(newNode);
}
How to access the extended information now in the custom TreeNode Class:
C#
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    TreeNodeEx selNode = treeView1.SelectedNode as TreeNodeEx;

    // set a break-point here and inspect the contents of 'selNode
}
You'll note that to access the extended information we had to cast the reference returned by treeView1.SelectedNode to our extended custom TreeNode Type. Without that cast there would be an error.

A nice feature of adding an instance of a Class to the custom TreeNode is that in Visual Studio when you inspect an instance of the TreeNode, you'll see the Class Name 'Information displayed separately in the pop-up, and you can open it easily to see the custom values.

You'll note the assumption made here that at the moment you create the TreeNodes you have, at hand, all the secondary information required to fill the fields of the instance of the 'Information class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900