TreeView控件显示Sql表数据 [英] TreeView Control displaying Sql Table Data

查看:72
本文介绍了TreeView控件显示Sql表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个TreeView控件.它将显示SQL表数据.
我有一张名为DealerDetails的表.
它包含DealerCode和位置.
我想要的是将Location用作父Node,将DealerCode用作ChildNode.
相同的位置DealerCode应该出现在位置"节点下.

我想使用SQL数据填充TreeView.

请帮助我.

解决方案

由于您的问题是开放式的,因此提供精确的解决方案可能会很困难.

如果您需要基于wpf或winforms的解决方案,则可以使用以下方法之一:

1.将所需的所有数据从sql server加载到数据集中

2.确保已定义所有关系(主/详细信息),这将更改层次结构显示中的帮助.
请参考 http://msdn.microsoft.com/en-us/library/ms171913.aspx [ ^ ]用于访问相关记录.

3.您需要一个树状视图控件,该控件可以显示多个列,并且应该能够在每个级别上显示不同的列.
为此,您可以在wpf中使用多级分层数据模板.

如果您想使用ASP.Net,则可以简单地使用多层嵌套GridView
请参考演练:创建嵌套的GridView控件 [ ^ ]

void fill_Tree2()
       {

           SqlConnection con = new SqlConnection();
           con.ConnectionString = Global.constr;
           con.Open();
           SqlCommand cmd2 = new SqlCommand("Select distinct station from Dealers order by station", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd2);
           DataSet PrSet = new DataSet();
           da.Fill(PrSet, "Dealers");

           treeView1.Nodes.Clear();

           foreach (DataRow dr in PrSet.Tables[0].Rows)
           {

               TreeNode tnParent = new TreeNode();

               tnParent.Text = dr["Station"].ToString();

               tnParent.Tag = dr["Station"].ToString();

               //   tnParent.PopulateOnDemand = true;

               //   tnParent.ToolTip = "Click to get Child";

               //   tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;

               tnParent.Expand();

               //   tnParent.Selected = true;

              treeView1.Nodes.Add(tnParent);

               FillChild(tnParent, tnParent.Tag.ToString());

           }
       }
       public void FillChild(TreeNode parent, string ParentId)
       {
           SqlConnection con = new SqlConnection();
           con.ConnectionString = Global.constr;
           con.Open();
           SqlCommand cmd2 = new SqlCommand("Select * from Dealers where Station =''" + ParentId+"''", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd2);
           DataSet PDataset = new DataSet();
           da.Fill(PDataset, "Dealers");
           DataSet ds = PDataset;

           parent.Nodes.Clear();

           foreach (DataRow dr in ds.Tables[0].Rows)
           {

               TreeNode child = new TreeNode();

               child.Text = dr["Code"].ToString().Trim();

               child.Tag = dr["Code"].ToString().Trim();

               if (child.Nodes.Count == 0)
               {

                   //child.PopulateOnDemand = true;

               }

               // child.ToolTip = "Click to get Child";

               // child.SelectAction =TreeNodeSelectAction.SelectExpand;

              // child.CollapseAll();

               parent.Nodes.Add(child);

           }

       }


I want to create a TreeView Control. It will Display the SQL Table Data.
I have one table Named as DealerDetails.
It contains DealerCode and Location.
What I want is for Location to be the parent Node and DealerCode to work as a ChildNode.
Same Locations DealerCode should appear under the Location Node.

I want to populate the TreeView using SQL Data.

Please help me.

解决方案

Since your question is openended, providing a precise solution could be difficult.

If you need a wpf or winforms based solution, this could be one of the many approach:

1. Load all the data you want from the sql server into datasets

2. Make sure you have all the relations defined on them (Master / detail), this would alter help in the hierarchical display.
Refer to http://msdn.microsoft.com/en-us/library/ms171913.aspx[^] for accessing related records.

3. You need a tree view control which can display multiple columns and should be able to display different columns at each level.
You can use multi level hierarchical data templates in wpf for this purpose.

If you wish to use ASP.Net you can simply use multi level nested GridView
Refer to Walkthrough: Creating a Nested GridView Control [^]


void fill_Tree2()
       {

           SqlConnection con = new SqlConnection();
           con.ConnectionString = Global.constr;
           con.Open();
           SqlCommand cmd2 = new SqlCommand("Select distinct station from Dealers order by station", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd2);
           DataSet PrSet = new DataSet();
           da.Fill(PrSet, "Dealers");

           treeView1.Nodes.Clear();

           foreach (DataRow dr in PrSet.Tables[0].Rows)
           {

               TreeNode tnParent = new TreeNode();

               tnParent.Text = dr["Station"].ToString();

               tnParent.Tag = dr["Station"].ToString();

               //   tnParent.PopulateOnDemand = true;

               //   tnParent.ToolTip = "Click to get Child";

               //   tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;

               tnParent.Expand();

               //   tnParent.Selected = true;

              treeView1.Nodes.Add(tnParent);

               FillChild(tnParent, tnParent.Tag.ToString());

           }
       }
       public void FillChild(TreeNode parent, string ParentId)
       {
           SqlConnection con = new SqlConnection();
           con.ConnectionString = Global.constr;
           con.Open();
           SqlCommand cmd2 = new SqlCommand("Select * from Dealers where Station =''" + ParentId+"''", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd2);
           DataSet PDataset = new DataSet();
           da.Fill(PDataset, "Dealers");
           DataSet ds = PDataset;

           parent.Nodes.Clear();

           foreach (DataRow dr in ds.Tables[0].Rows)
           {

               TreeNode child = new TreeNode();

               child.Text = dr["Code"].ToString().Trim();

               child.Tag = dr["Code"].ToString().Trim();

               if (child.Nodes.Count == 0)
               {

                   //child.PopulateOnDemand = true;

               }

               // child.ToolTip = "Click to get Child";

               // child.SelectAction =TreeNodeSelectAction.SelectExpand;

              // child.CollapseAll();

               parent.Nodes.Add(child);

           }

       }


这篇关于TreeView控件显示Sql表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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