Click here to Skip to main content
12,818,438 members (36,002 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
i populated the treeview from Common table structure from sql in window forms
i have to show the after the click on parent node in treeview the related child show
in listview.i set for parent and child. [flag id flag zero means he is parent and flag 1 means he have parent. ]so i dont know how to do plz tell can i do it???so how can i show after clicked on parent node in treeview and related child in list view.i am new for C# programing.
i have one table name in sql is tblHead in that i have four field (headid,headname,flag,type)
here is my code:
i populated treeview in leftside in windows foms but in the rightside i have listview in that i have to show child .when i click the parent node in treeview .so i can i do plz tell me???
private void Form1_Load(object sender, EventArgs e)
            Connection c = new Connection();
            string con1 = c.getconnstr();
            con.ConnectionString = con1;

            //Connection Open Here
            // Clear the dataset
            string str = "select * from tblhead";
            SqlDataAdapter da = new SqlDataAdapter(str, con);

            //DataSet Fill Here
            da.Fill(ds, "Parent");

            //Call the function and pass treeview node,parent element and table
            CreateTreeView(treeView1.Nodes, 0, ds.Tables[0]);
        protected void CreateTreeView(TreeNodeCollection parentNode, int parentID, DataTable mytab)
            foreach (DataRow dta in mytab.Rows)
                if (Convert.ToInt32(dta["flag"]) == parentID)
                    String key = dta["headid"].ToString();
                    String text = dta["headname"].ToString();
                    TreeNodeCollection newParentNode = parentNode.Add(key, text).Nodes;
                    CreateTreeView(newParentNode, Convert.ToInt32(dta["headid"]), mytab);
Posted 21-Feb-12 21:07pm
Updated 21-Feb-12 23:21pm
Mateen Khan 22-Feb-12 2:49am
Hi Sadhana,
Please if you can give more details about what you actually want to do.Do paste some of the code that you have done.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You have to write your code on the NodeMouseClick event of the tree view.
1)You can loop through the node to get all the child data.
2)Keep the child tag and data in some collection say datatable.
3) After the loop bind that datatable with the listview.

 private void Form1_Load(object sender, EventArgs e)

            listView1.View = View.Details;	            
            listView1.Columns.Add("Node Name", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("Node Value", 100, HorizontalAlignment.Left);

private void tvItems_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
            TreeNode tn = e.Node;

            //create a datatable with two columns
            DataTable aTable = new DataTable();
            aTable.Columns.Add("Headid", typeof(string));
            aTable.Columns.Add("HeadName", typeof(string));

            //in this for each loop we will traverse through the clicked tree node 
            // and get all child nodes name and tag(value) and keep it in datarow 
            // of our datatable .
            foreach (TreeNode t1 in tn.Nodes)
                DataRow dr;
                dr = aTable.NewRow();
                dr[0] = t1.Text;
                dr[1] = t1.Tag.ToString();


         //finally we will make datatabe as the source of listview
          //for listview we have to add listviewitem
             ListViewItem ii;
            foreach (DataRow rr in aTable.Rows)
                   ii =listView1.Items.Add(rr[0].ToString());

So here's the whole bunch of code. Now with this code you will definetly get things right.
Do let me know if you face any further problem.
sadhana4 23-Feb-12 2:01am
how to bound the datasource to the listview.listview.datasource is not wroking in windows
forms.i did nt get right output.plz tell me how to do
Mateen Khan 23-Feb-12 5:46am
I've improved the solution. Added the code for populating Listview from datatable :).
sadhana4 23-Feb-12 8:03am
but tag value showing null .
how to show populating listview from datatable
sadhana4 23-Feb-12 9:36am
plz tell me how can i do???
sadhana4 24-Feb-12 0:44am
i cannot get right result bcoz in the tag doest take value.means tag showing null value
what should i do??
Mateen Khan 24-Feb-12 6:11am
in your case instead of tag use name property i.e this will get you the desired Headid and t1.text will get you the headname.
You can see the properties of TreeNode by selecting the tree node object and pressing ctrl+alt+Q to open Quickwatch window and view all the properties.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 23 Feb 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100