填充6级树视图和Costumized [英] Populating 6 level Tree View and Costumized

查看:99
本文介绍了填充6级树视图和Costumized的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我真的需要帮助..

我想填充一个6级的树视图,数据来自数据库。



Wanted Treeview

- 父母

- 类别

- 类型

- 子类型

- 系统

- 型号



表1:TabelParent

| ID |家长|



表2:TabelCategory

| ID |类别| ParID | // ParID - >外键



表3:TabelType

| ID |输入| CatID |



与表4和5混淆



表6:TabelModel

| ParID | CatID | TypeID | SubID | SysID | ModelID | Model_Name |





每个模特都有父母,类别和类型,

,问题是,并非每个模型都有SubType或System(可以是两者)..

所以它可以是完成没有子类型没有系统没有子类型和没有系统 ..



i我试图这样做

Guys, i really need help..
I want to populate a treeview that has 6 level and data was taken from database.

Wanted Treeview
- Parent
- Category
- Type
- Sub-Type
- System
- Model

Table 1 : TabelParent
| ID | Parent |

Table 2 : TabelCategory
| ID | Category | ParID | //ParID -> Foreign key

Table 3 : TabelType
| ID | Type | CatID |

confused with table 4 and 5

Table 6 : TabelModel
| ParID | CatID | TypeID | SubID | SysID | ModelID | Model_Name |


Every Model has Parent, category and Type,,
and the problem is, Not every Model has SubType or System (can be both)..
so it can be Complete or no Sub-Type or no System or no SubType and no System..

i have tried to make it like this

public partial class Form3 : Form
    {
        SqlConnection cn = new SqlConnection(@"Data Source=SAMMY-PC\SQLEXPRESS;Initial Catalog=Prod_Knowledge Nayati;Integrated Security=True");
        public Form3()
        {
            InitializeComponent();
            tree();
        }

        private void tree()
        {
            treeView1.Nodes.Clear();
            cn.Open();
            SqlDataAdapter Par = new SqlDataAdapter("Select * FROM TabelParent", cn);
            SqlDataAdapter Cat = new SqlDataAdapter("SELECT * FROM TabelCategory", cn);
            SqlDataAdapter Type = new SqlDataAdapter("SELECT * FROM TabelType", cn);
            SqlDataAdapter Sub = new SqlDataAdapter("SELECT * FROM TabelSubType", cn);
            SqlDataAdapter Sys = new SqlDataAdapter("SELECT * FROM TabelSystem", cn);
            SqlDataAdapter Mod = new SqlDataAdapter("SELECT * FROM TabelModel", cn);
            DataSet ds = new DataSet();
            Par.Fill(ds, "TabelParent");
            Cat.Fill(ds, "TabelCategory");
            Type.Fill(ds, "TabelType");
            Sub.Fill(ds, "TabelSubType");
            Sys.Fill(ds, "TabelSystem");
            Mod.Fill(ds, "TabelModel");
            //DataColumn dc1 = ds.Tables["TabelCategory"].Columns["Index"];

            //DataColumn dc2 = ds.Tables["TabelModel"].Columns["Index1"];
            //DataColumn dc3 = ds.Tables["TabelType"].Columns["Index"];
            //DataColumn dc4 = ds.Tables["TabelModel"].Columns["Index2"];

            //DataRelation dr1 = new DataRelation("CatIndex", dc1, dc2);
            //DataRelation dr2 = new DataRelation("TypeIndex", dc3, dc4);

            //ds.Relations.Add(dr1);
            //ds.Relations.Add(dr2);

            foreach(DataRow dat1 in ds.Tables["TabelParent"].Rows)
            {//1 Parent
                TreeNode tn1 = new TreeNode(dat1["Parent"].ToString());
                foreach (DataRow dat2 in ds.Tables["TabelCategory"].Rows)
                {//2 Category
                    TreeNode tn2 = new TreeNode(dat2["Category"].ToString());
                    if (dat1["ID"].ToString() == dat2["ParentID"].ToString())
                    {// if P.Parent = F.Par - Cat
                        foreach (DataRow dat3 in ds.Tables["TabelType"].Rows)
                        {//3 Type
                            TreeNode tn3 = new TreeNode(dat3["Type"].ToString());
                            if (dat2["ID"].ToString() == dat3["CatID"].ToString())
                            {// if P.Cat = F.Cat - Type
                                foreach(DataRow dat4 in ds.Tables["TabelSubType"].Rows)
                                {//4 SubType
                                    TreeNode tn4 = new TreeNode(dat4["SubType"].ToString());
                                    if (dat3["ID"].ToString() == dat4["TypeID"].ToString())
                                    {
                                        // THE MESS STARTED HERE
                                        foreach(DataRow dat6 in ds.Tables["TabelModel"].Rows)
                                        {//5 Model
                                            TreeNode tn6 = new TreeNode(dat6["Model"].ToString());
                                            if (dat6["SubID"].ToString() == "0")
                                            {
                                                TreeNode tn5; 
                                                foreach (DataRow dat5 in ds.Tables["TabelSystem"].Rows)
                                                {
                                                    
                                                    if (dat5["ID"].ToString() == dat6["SysID"].ToString())
                                                    {
                                                        tn5 = new TreeNode(dat5["System"].ToString());
                                                        tn3.Nodes.Add(tn5);
                                                        tn5.Nodes.Add(tn6);
                                                    }
                                                }
                                                
                                            }
                                            //tn4.Nodes.Add(tn6);


                                        }//5 Model
                                        tn3.Nodes.Add(tn4);
                                    }


                                    
                                }//4 SubType

                                tn2.Nodes.Add(tn3);
                            }// if Type
                        }//3 Type
                        tn1.Nodes.Add(tn2);
                    }// if cat
                }//2 Category
                treeView1.Nodes.Add(tn1);
            }//1 Parent
        }
    }





i可以生成直到Sub ,,但是系统等等一团糟......

所以如果有人可以提供帮助,我会感激不尽..



i can generate until Sub,, but the system and so on was a mess..
So if anyone can help, i'd be thankfull..

推荐答案

是的,所有这一切都是灾难。如果你正在为某人做这件事,他们会注意到你拼写了表格'tabel',但是,为什么要为每个表名添加任何表格?



我会完全不同的做法。我会读取顶级数据,然后有一个方法插入每个顶级项目,然后在该方法内部运行SQL以查找该级别的子项目。然后调用一个方法来插入每个新级别,再次执行相同的操作。或者我写了一个SQL语句,它返回了我需要的所有数据,按父节点详细信息分组,所以我可以迭代它来构建我的数据。
Yes, all of this is a disaster. If you're doing this for someone, they will notice that you spelled table, 'tabel', but, why preface each table name with it any how ?

I would do it completely differently. I would read the data for the top level, then have a method that inserts each top level item, and then inside that method, run SQL to find sub items for that level. then a method is called to insert each new level, which does the same thing again. Or I'd write one SQL statement that returned all the data I needed, grouped by parent node details so I could just iterate over it to build my data.


这篇关于填充6级树视图和Costumized的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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