Tôi có một bảng cơ sở dữ liệu (tên chủ đề) trong đó bao gồm các lĩnh vực:cư TreeView từ DataBase
- topicId
- tên
- ParentID
và bằng cách sử dụng chúng tôi muốn cư một TreeView trong C#. Làm thế nào tôi có thể làm điều đó ?
Cảm ơn trước ...
Tôi có một bảng cơ sở dữ liệu (tên chủ đề) trong đó bao gồm các lĩnh vực:cư TreeView từ DataBase
và bằng cách sử dụng chúng tôi muốn cư một TreeView trong C#. Làm thế nào tôi có thể làm điều đó ?
Cảm ơn trước ...
Nó có thể sẽ giống như thế này. Cung cấp cho một số chi tiết hơn như những gì chính xác bạn muốn làm gì nếu bạn cần nhiều hơn nữa.
//In Page load
foreach (DataRow row in topics.Rows)
{
TreeNode node = new TreeNode(dr["name"], dr["topicId"])
node.PopulateOnDemand = true;
TreeView1.Nodes.Add(node);
}
///
protected void PopulateNode(Object sender, TreeNodeEventArgs e)
{
string topicId = e.Node.Value;
//select from topic where parentId = topicId.
foreach (DataRow row in topics.Rows)
{
TreeNode node = new TreeNode(dr["name"], dr["topicId"])
node.PopulateOnDemand = true;
e.Node.ChildNodes.Add(node);
}
}
Không hoàn toàn.
Cây thường được xử lý tốt nhất bằng cách không tải mọi thứ bạn có thể cùng một lúc. Vì vậy, bạn cần phải có được nút gốc (hoặc chủ đề) mà không có parentID. Sau đó, thêm chúng vào nút gốc cây và sau đó cho mỗi nút bạn thêm bạn cần để có được con của nó.
foreach (DataRow row in topicsWithOutParents.Rows)
{
TreeNode node = New TreeNode(... whatever);
DataSet childNodes = GetRowsWhereParentIDEquals(row["topicId"]);
foreach (DataRow child in childNodes.Rows)
{
Treenode childNode = new TreeNode(..Whatever);
node.Nodes.add(childNode);
}
Tree.Nodes.Add(node);
}
Khi không có số lượng lớn dữ liệu thì không tốt để kết nối cơ sở dữ liệu, tìm nạp dữ liệu và thêm vào nút treeview một lần nữa cho nút con/phụ. Nó có thể được thực hiện trong một lần thử. Xem mẫu sau:
http://urenjoy.blogspot.com/2009/08/display-hierarchical-data-with-treeview.html
mã này chạy hoàn hảo đối với tôi, kiểm tra xem nó ra tôi nghĩ rằng nó sẽ giúp bạn :)
;
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = RunQuery("Select topicid,name from Topics where Parent_ID IS NULL");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode root = new TreeNode(ds.Tables[0].Rows[i][1].ToString(),ds.Tables[0].Rows[i][0].ToString());
root.SelectAction = TreeNodeSelectAction.Expand;
CreateNode(root);
TreeView1.Nodes.Add(root);
}
}
void CreateNode(TreeNode node)
{
DataSet ds = RunQuery("Select topicid, name from Category where Parent_ID =" + node.Value);
if (ds.Tables[0].Rows.Count == 0)
{
return;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode tnode = new TreeNode(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString());
tnode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(tnode);
CreateNode(tnode);
}
}
DataSet RunQuery(String Query)
{
DataSet ds = new DataSet();
String connStr = "???";//write your connection string here;
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand objCommand = new SqlCommand(Query, conn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
da.Fill(ds);
da.Dispose();
}
return ds;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateRootLevel();
}
private void PopulateRootLevel()
{
SqlConnection objConn = new SqlConnection(connStr);
SqlCommand objCommand = new SqlCommand(@"select FoodCategoryID,FoodCategoryName,(select count(*) FROM FoodCategories WHERE ParentID=c.FoodCategoryID) childnodecount FROM FoodCategories c where ParentID IS NULL", objConn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView2.Nodes);
}
private void PopulateSubLevel(int parentid, TreeNode parentNode)
{
SqlConnection objConn = new SqlConnection(connStr);
SqlCommand objCommand = new SqlCommand(@"select FoodCategoryID,FoodCategoryName,(select count(*) FROM FoodCategories WHERE ParentID=sc.FoodCategoryID) childnodecount FROM FoodCategories sc where [email protected]", objConn);
objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
}
private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["FoodCategoryName"].ToString();
tn.Value = dr["FoodCategoryID"].ToString();
nodes.Add(tn);
//If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
}
}
Bài cũ, nhưng rất hữu ích. Dù sao, làm cách nào tôi có thể nhận được giá trị khi nút được nhấp? Chúc mừng, – Haminteu