treeview dynamically populated
I have this treeview control where I want to put uploaded files on the server. I want to be able to create the nodes and the child nodes dynamically from the database. I am using this query for getting the data from DB:
SELECT c.Category, d.DocumentName FROM Categories c
INNER JOIN DocumentUserFile d
ON c.ID = d.CategoryId
WHERE d.UserId = '9rge333a-91b5-4521-b3e6-dfb49b45237c'
The result from that query is this one:
Agendas
transactions.pdf
Minutes
accounts.pdf
I want to have the treeview sorted that way too. I am trying with this piece of code:
TreeNode tn = new TreeNode();
TreeNode tnSub = new TreeNode();
foreach (DataRow开发者_JS百科 dt in tblTreeView.Rows)
{
tn.Text = dt[0].ToString();
tn.Value = dt[0].ToString();
tnSub.Text = dt[1].ToString();
tnSub.NavigateUrl = "../downloading.aspx?file=" + dt[1].ToString() +"&user=" + userID;
tn.ChildNodes.Add(tnSub);
tvDocuments.Nodes.Add(tn);
}
I am getting the treeview populated nicely for the 1st category and the document under that category, but I can't get it to work when I want to show more documents under that category, or even more complicate to show new category beneath the 1st one with documents from that category. How can I solve this? I appreciate the answers a lot. Thanks, Laziale
You should create new nodes for each item:
// TreeNode tn = new TreeNode();
// TreeNode tnSub = new TreeNode();
foreach (DataRow dt in tblTreeView.Rows)
{
TreeNode tn = new TreeNode(); // *
tn.Text = dt[0].ToString();
tn.Value = dt[0].ToString();
TreeNode tnSub = new TreeNode(); // *
tnSub.Text = dt[1].ToString();
tnSub.NavigateUrl = "../downloading.aspx?file=" + dt[1].ToString() +"&user=" + userID;
tn.ChildNodes.Add(tnSub);
tvDocuments.Nodes.Add(tn);
}
精彩评论