Reputation: 435
I am using TreeView in my project to hierarchical display of my data from database. It works fine for small amount of data, say 500 entries. But when it exceeds that number of entries, it is taking too much time for loading. I have to populate large amount of data (say 2500 entries). I have one table named as "tRegistered" from where I bind "Date" column as treeview Parent Node. And again from the same table I bind "Users" column as treeview Node. My objective is to display all registered users for the last 7 days in a hierarchical way. Please help me how do I proceed.
My code is:
private void PopulateTreeView()
{
treeView1.Nodes.Clear();
SqlDataAdapter daPatient = new SqlDataAdapter("SELECT TOP 100 PERCENT pId, pDate, pName FROM tRegistered WHERE pDate >= DATEADD(day,-7, GETDATE())", con);
SqlDataAdapter daDate = new SqlDataAdapter("SELECT TOP 100 PERCENT pDate FROM tRegistered
WHERE pDate >= DATEADD(day, - 7, GETDATE()))
GROUP BY pDate
ORDER BY pDate DESC", con);
DataSet ds = new DataSet();
daPatient.Fill(ds, "tRegistered");
daDate.Fill(ds, "tRegistered");
//Add root node
TreeNode root = new TreeNode("Registered");
treeView1.Nodes.Add(root); //Hard code
ds.Relations.Add("Regsd", ds.Tables["tRegistered"].Columns["pDate"], ds.Tables["tRegistered"].Columns["pDate"]);
foreach (DataRow dr in ds.Tables["tRegistered"].Rows)
{
DateTime dt = Convert.ToDateTime(dr["pDate"]);
TreeNode tn = new TreeNode(String.Format("{0:dd-MMM-yyyy}", dt));
foreach (DataRow drChild in dr.GetChildRows("Regsd"))
{
TreeNode childTn = new TreeNode(drChild["pId"].ToString() + "- " + drChild["pName"].ToString());
childTn.Tag = drChild["pId"];
tn.Nodes.Add(childTn);
}
root.Nodes.Add(tn);
root.Expand();
}
}
Upvotes: 1
Views: 636
Reputation: 2020
Use TreeView1.BeginUpdate() and EndUpdate() which prevent the GUI from being constantly updated.
If you have many sub levels, you may want to do childTN.Expand() only for a few levels, or even just expand the Date nodes (tn).
TreeNode root = new TreeNode("Registered");
root.Expand();
ds.Relations.Add("Regsd", ds.Tables["tRegistered"].Columns["pDate"], ds.Tables["tRegistered"].Columns["pDate"]);
foreach (DataRow dr in ds.Tables["tRegistered"].Rows)
{
DateTime dt = Convert.ToDateTime(dr["pDate"]);
TreeNode tn = new TreeNode(String.Format("{0:dd-MMM-yyyy}", dt));
tn.Expand();
foreach (DataRow drChild in dr.GetChildRows("Regsd"))
{
TreeNode childTn = new TreeNode(drChild["pId"].ToString() + "- " + drChild["pName"].ToString());
childTn.Tag = drChild["pId"];
childTN.Expand();
tn.Nodes.Add(childTn);
}
root.Nodes.Add(tn);
//root.Expand();
}
TreeView1.BeginUpdate();
TreeView1.Nodes.Add(root); //Hard code
TreeView1.EndUpdate();
Edit: Changed the text and the code snipet.
Upvotes: 3
Reputation: 16443
Try adding the root node to the TreeView control after you have loaded all the child nodes into it. You should see a massive speed improvement.
Upvotes: 0