user3802347
user3802347

Reputation: 108

Panel Does not update TreeView

I have a TreeView that is displayed inside a panel. The data in the TreeView is based on data returned from the database. The first time, the data is correct. The second time, the TreeView is not refreshed, and the previous data is still showing in the tree. I checked the list that contain the data. The list returned the correct data. I've Google the issue, and could not resolved it with some of the answers that were posted. Here is a sample code of how the TreeView is being created and added to the Panel.

    ReportGroups gr = new ReportGroups();
    var Name = gr.GetReportName(groupID);
    TreeView tr = new TreeView();
    tr.BeginUpdate();
    tr.Size = new Size(570, 600);
    tr.Name = "Home";
    tr.Nodes.Add("Reports Name");
    tr.CheckBoxes = true;
    if (Name.Count() > 0)
    {
        foreach (var item in Name)
        {
            if (item != null)
            {
                tr.Nodes[0].Nodes.Add(item.reportName);
            }
        }
    }
    tr.Nodes[0].ExpandAll();
    tr.EndUpdate();
    this.pDisplayReportName.Width = tr.Width * 2;
    this.pDisplayReportName.Height = 300;
    this.pDisplayReportName.Controls.Add(tr);
    this.pDisplayReportName.Refresh();   

What am I doing wrong?

Upvotes: 0

Views: 182

Answers (2)

bumbumpaw
bumbumpaw

Reputation: 2530

try to add this.pDisplayReportName.Clear(); so data will not double up. :)

Upvotes: 1

Sam
Sam

Reputation: 2917

The easy option would be to use this.pDisplayReportName.Controls.Clear(); just after tr.EndUpdate();. But, this would cause an issue if you have other controls within the same Panel.

The best option would be to use this.pDisplayReportName.Controls.RemoveByKey("MyTree"); instead of this.pDisplayReportName.Controls.Clear();

And, another option would be to add a TreeView in design time (with name tr) rather than dynamically to the panel. Then, use tr.Nodes.Clear(); before tr.BeginUpdate(); and remove following two lines from your code.

TreeView tr = new TreeView();
.
.
.
this.pDisplayReportName.Controls.Add(tr);

Cheers

Upvotes: 1

Related Questions