从数据库填充TreeView控件






4.58/5 (20投票s)
2006 年 7 月 13 日
4分钟阅读

256913

8518
如何从数据库填充 TreeView 控件。
引言
ASP.NET 2.0 提供了大量新控件,可帮助开发人员加速开发。 在所有新控件中,TreeView
控件是最重要和最有用的控件之一。 TreeView
控件使您可以分层形式显示数据。 TreeView
控件还允许您在其中嵌入不同的 ASP.NET 控件。 在本文中,我们将了解如何从数据库填充 TreeView
控件。
设置场景
可以使用不同类型的源填充 TreeView
控件,包括 SiteMapDataSource
控件、XML 文件、集合、容器和数据库表。 数据源的选择与应用程序的场景和需求密切相关。 当您确定数据会不断变化时,数据库应该是首选。 当您想要对数据执行不同的操作时,它也是首选。 这些操作包括排序、搜索和排序。
在本文中,我将使用 Northwind 数据库,它是 SQL Server 7 和 SQL Server 2000 的默认数据库。 我将处理 Northwind 数据库中的 Categories 和 Products 表。 由于 TreeView
主要用于显示分层数据,因此我将显示 Category 表中的项目以及属于每个类别的所有产品。 我将使用实体类和泛型集合来创建 Category
类和 Product
类之间的关系。 首先,让我们看看将从 Categories 和 Products 表返回结果的 T-SQL 查询。
T-SQL 查询
以下 T-SQL 查询用于获取有关类别和产品的信息。 您可以轻松地将查询更改为存储过程,但我想保持简单,这就是我没有实现存储过程的原因。
SELECT p.ProductID, p.ProductName,c.CategoryID,c.CategoryName
FROM Products p
JOIN Categories c ON p.CategoryID = c.CategoryID
ORDER BY c.CategoryID
创建实体类
下一步是为 Category 和 Products 表创建实体类。 我创建了一个小工具,可以用来创建实体类。 您可以在下面查看实体类的代码
public class Product
{
private int productID;
private string productName;
public int ProductID
{
get { return this.productID; }
set { this.productID = value; }
}
public string ProductName
{
get { return this.productName; }
set { this.productName = value; }
}
public Product(int productID, string productName)
{
this.productID = productID;
this.productName = productName;
}
public Product()
{
}
}
这是 Category
实体类
public class Category
{
private int categoryID;
private string categoryName;
private List<Product> productList =
new List<Product>();
public int CategoryID
{
get { return this.categoryID; }
set { this.categoryID = value; }
}
public string CategoryName
{
get { return this.categoryName; }
set { this.categoryName = value; }
}
public List<Product> ProductList
{
get { return this.productList; }
set { this.productList = value; }
}
创建实体类后,下一步是填充泛型类别列表。
填充泛型类别列表
现在,让我们看看如何创建和填充泛型类别列表。 创建泛型类别列表很简单,可以通过一行代码完成。
List<Category> categoryList = new List<Category>();
现在,让我们看看如何填充类别列表。 填充类别列表背后的想法是,一个列表可以有多个类别,每个类别可以有多个产品。 换句话说,泛型列表将包含类别对象,并且每个单个类别对象将包含产品对象列表。
int i = -1;
while (reader.Read())
{
Category category = new Category();
category.CategoryID = Convert.ToInt32(reader["CategoryID"]);
category.CategoryName = reader["CategoryName"] as String;
if (!DoesCategoryIDAlreadyExists(category.CategoryID, categoryList))
{
categoryList.Add(category);
i++;
categoryList[i].productList.Add(new Product(
Convert.ToInt32(reader["ProductID"]),
reader["ProductName"] as String));
}
else
{
categoryList[i].productList.Add(new Product(
Convert.ToInt32(reader["ProductID"]),
reader["ProductName"] as String));
}
}
完整的代码可以在下载中找到。 DoesCategoryIDAlreadyExists
检查我们是否正在处理相同的类别。 如果是,那么我们只需继续将产品添加到该特定类别。 上述方法可能不是填充类别列表的最佳方法,如果您想出一些有趣的东西,请告诉我。 无论如何,在填充类别列表之后,下一步是填充 TreeView
控件。
填充 TreeView 控件
由于数据来自数据库,因此我们必须动态构建 TreeNode
和相应的 ChildNode
。 这个想法是循环遍历类别并创建父节点,并循环遍历相应的子节点以创建产品。
// This method is used to populate the TreeView Control
private void PopulateTreeViewControl(List<Category> categoryList)
{
TreeNode parentNode = null;
foreach (Category category in categoryList)
{
parentNode = new TreeNode(category.CategoryName,
category.CategoryID.ToString());
foreach (Product product in category.ProductList)
{
TreeNode childNode = new TreeNode(product.ProductName,
product.ProductID.ToString());
parentNode.ChildNodes.Add(childNode);
}
parentNode.Collapse();
// Show all checkboxes
tvCategories.ShowCheckBoxes = TreeNodeTypes.All;
tvCategories.Nodes.Add(parentNode);
}
}
PopulateTreeViewControl
方法将类别列表作为参数,并迭代以填充 TreeView
控件。 TreeNode
的 Collapse
方法负责保持 TreeView
紧凑,以便不展开叶子。 最后,在创建 TreeNode
和 ChildNode
之后,将节点添加到 TreeView
的 Nodes
集合中。
看看下面的截图
结论
在本文中,我们学习了如何使用来自数据库的数据填充 TreeView
控件。 实体类的使用简化了开发并减少了代码行数。 在以后的文章中,我们将看到如何使用 TreeView
控件内的复选框选择节点。
希望您喜欢这篇文章,祝您编码愉快!