zoukankan      html  css  js  c++  java
  • 在 TreeView 控件中显示分层数据

    1、在web.config文件中创建以下数据库连接字符串
    <connectionStrings>
            
    <add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True"
                providerName
    ="System.Data.SqlClient" />
        
    </connectionStrings>
    2、在Treeview页面中添加以下代码
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            
    if (e.Node.ChildNodes.Count == 0)
            {
                
    switch (e.Node.Depth)
                {
                    
    case 0:
                        PopulateCategories(e.Node);
                        
    break;
                    
    case 1:
                        PopulateProducts(e.Node);
                        
    break;
                }
            }
        }

    当用户单击一个节点以打开该节点时,会调用此代码。因为需要在树的不同级别显示不同的数据,所以必须确定用户单击的节点深度,然后适当地填充该级别的节点。在此演练中,如果用户单击根节点(深度为 0),则调用 PopulateCategories 方法。如果用户单击类别名称(深度为 1),则调用 PopulateProducts 方法。这些方法在下一节中演示。
    TreeNodeEventArgs 对象提供对当前节点的编程访问。若要填充节点,请向节点添加元素。在该代码示例中,节点被传递至方法,该方法将添加子节点。

    void PopulateCategories(TreeNode node)
        {
            SqlCommand sqlQuery 
    = new SqlCommand(
                
    "Select CategoryName, CategoryID From Categories");
            DataSet resultSet;
            resultSet 
    = RunQuery(sqlQuery);
            
    if (resultSet.Tables.Count > 0)
            {
                
    foreach (DataRow row in resultSet.Tables[0].Rows)
                {
                    TreeNode NewNode 
    = new
                        TreeNode(row[
    "CategoryName"].ToString(),
                        row[
    "CategoryID"].ToString());
                    NewNode.PopulateOnDemand 
    = true;
                    NewNode.SelectAction 
    = TreeNodeSelectAction.Expand;
                    node.ChildNodes.Add(NewNode);
                }
            }
        }

    该代码创建 SqlCommand 对象,该对象封装查询的文本。代码将该对象传递至一个随后将要编写的方法,该方法执行数据库查询,并返回 DataSet 对象。此代码然后遍历 DataSet 对象中的记录,并为每条记录创建一个新的节点,以数据库信息设置该节点的文本和值。然后,代码将每个节点的 PopulateOnDemand 属性设置为 true,以使节点在被单击时将引发其 TreeNodePopulate 事件。SelectAction 属性被设置,以使节点在默认情况下展开。

    第二级别的节点将显示每个类别的产品。由于此原因,填充产品节点需要参数化查询,以使您能够检索当前类别的产品,并以恰当方式填充子节点。

    void PopulateProducts(TreeNode node)
        {
            SqlCommand sqlQuery 
    = new SqlCommand();
            sqlQuery.CommandText 
    = "Select ProductName From Products " +
                
    " Where CategoryID = @categoryid";
            sqlQuery.Parameters.Add(
    "@categoryid", SqlDbType.Int).Value =
                node.Value;
            DataSet ResultSet 
    = RunQuery(sqlQuery);
            
    if (ResultSet.Tables.Count > 0)
            {
                
    foreach (DataRow row in ResultSet.Tables[0].Rows)
                {
                    TreeNode NewNode 
    = new
                        TreeNode(row[
    "ProductName"].ToString());
                    NewNode.PopulateOnDemand 
    = false;
                    NewNode.SelectAction 
    = TreeNodeSelectAction.None;
                    node.ChildNodes.Add(NewNode);
                }
            }
        }
    此代码与用以填充类别节点的代码类似。不同之一是 SqlCommand 对象配置有一个参数,在运行时,以用户单击的节点(即选择的类别)的值来设置该参数。另一不同之处是 PopulateOnDemand 属性设置为 false。这导致产品节点显示后不带有展开按钮,这是必须的,因为产品下再没有节点。

    private DataSet RunQuery(SqlCommand sqlQuery)
        {
            
    string connectionString =
                ConfigurationManager.ConnectionStrings
                [
    "NorthwindConnectionString"].ConnectionString;
            SqlConnection DBConnection 
    =
                
    new SqlConnection(connectionString);
            SqlDataAdapter dbAdapter 
    = new SqlDataAdapter();
            dbAdapter.SelectCommand 
    = sqlQuery;
            sqlQuery.Connection 
    = DBConnection;
            DataSet resultsDataSet 
    = new DataSet();
            
    try
            {
                dbAdapter.Fill(resultsDataSet);
            }
            
    catch
            {
                labelStatus.Text 
    = "Unable to connect to SQL Server.";
            }
            
    return resultsDataSet;
        }
  • 相关阅读:
    【python】 time模块和datetime模块详解 【转】
    【python 】装饰器 (多个参数的函数,带参数的装饰器)【转】
    从测试角度对测试驱动开发的思考【转】
    mysql性能优化-慢查询分析、优化索引和配置【转】
    【python 】Requests 库学习笔记
    二本院校计算机专业考研上岸985
    mysql创建触发器
    pat 1134 Vertex Cover (25分) 超时问题
    数据库三级封锁协议简述
    pat 1139 First Contact (30分) 题目详解
  • 原文地址:https://www.cnblogs.com/qixin622/p/769650.html
Copyright © 2011-2022 走看看