Click here to Skip to main content
Rate this: bad
good
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 me.how 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
            con.Open();
            // Clear the dataset
            ds.Clear();
            string str = "select * from tblhead";
            SqlDataAdapter da = new SqlDataAdapter(str, con);
 
            //DataSet Fill Here
            da.Fill(ds, "Parent");
            
            treeView1.BeginUpdate();
            treeView1.Nodes.Clear();
 
            //Call the function and pass treeview node,parent element and table
            CreateTreeView(treeView1.Nodes, 0, ds.Tables[0]);
            //treeView1.Nodes[0].Expand();
            treeView1.Select();
            treeView1.EndUpdate();
            con.Close();
        }
        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
Edited 21-Feb-12 23:21pm
v4
Comments
Mateen Khan at 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
good
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)
        {
            listView1.Items.Clear();
            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();
                aTable.Rows.Add(dr);
 
                
            }
 
         //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());
                   ii.SubItems.Add(rr[1].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.
  Permalink  
v2
Comments
sadhana4 at 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 at 23-Feb-12 5:46am
   
I've improved the solution. Added the code for populating Listview from datatable :).
sadhana4 at 23-Feb-12 8:03am
   
but tag value showing null .
how to show populating listview from datatable
sadhana4 at 23-Feb-12 9:36am
   
plz tell me how can i do???
sadhana4 at 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 at 24-Feb-12 6:11am
   
in your case instead of tag use name property i.e
t1.name 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
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 23 Feb 2012
Copyright © CodeProject, 1999-2014
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