C#Windows窗体Treeview和ListView [英] C# windows forms treeview and listview

查看:89
本文介绍了C#Windows窗体Treeview和ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以窗口形式从sql的Common table结构中填充了树形视图
我必须在树形视图中单击父节点后显示相关的子项显示
在listview.i中为父母和孩子设置. [flag id标志零表示他是父母,标志1表示他有父母. ]所以我不知道该怎么办,请告诉我.怎么办?在单击Treeview的父节点和列表视图中的相关子节点后,如何显示.我是C#编程的新手.
我在sql中有一个表名是 tblHead ,因为我有四个字段( headid,headname,flag,type )
这是我的代码:
我在Windows FOM的左侧填充了树视图,但在右侧,我必须显示子视图.当我单击树视图中的父节点时,我可以显示子视图.所以我可以告诉我吗?

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);
                }
            }
        }

推荐答案

您必须在树视图的NodeMouseClick事件上编写代码.
1)您可以遍历节点以获取所有子数据.
2)保留子标签和某些数据集中的数据,例如datatable.
3)循环后,将该数据表与listview绑定.

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.


这篇关于C#Windows窗体Treeview和ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆