public static void trBD(TreeView TrV)
{
try
{
DataTable treedt;
string str = “”;
TrV.Font = new Font(“宋体”, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
//设置树的父节点
TreeNode RootNode = TrV.Nodes.Add(“A”, “按级别分”, 1);
//格式:
//treeview.nodes.add(“节点名称(NAME)”,”节点显示的文本(TEXT)”,1,2);
//其中第三个参数为树节点中显示图像索引;第四个参数为处于选定状态的树节点图像索引
RootNode.ImageIndex = 1;
RootNode.SelectedImageIndex = 1;
//设置子节点
str = “Select * from member where class=”A”and status= “T””;
treedt =Dboperate.getDataTable(str);
for (int i = 0; i < treedt.Rows.Count; i++)
{
TreeNode RootNode2 = new TreeNode();
RootNode2.Text = treedt.Rows[i][“name”].ToString().Trim();
RootNode.Nodes.Add(RootNode2);
string s = “select * from member where h_level_id=”” + treedt.Rows[i][“pid”].ToString().Trim() + “”and Class=”B” and status= “T””;
DataTable dtcity = Dboperate.getDataTable(s);
for (int l = 0; l < dtcity.Rows.Count; l++)
{
string ChildNodes = dtcity.Rows[l][“name”].ToString();
RootNode2.Nodes.Add(ChildNodes);
RootNode2.SelectedImageIndex = 1;
s = “select * from member where h_level_id=” + dtcity.Rows[l][“pid”].ToString() + “”;
DataTable dtleader = Dboperate.getDataTable(s);
for (int h = 0; h < dtleader.Rows.Count; h++)
{
TreeNode RootNode3 = new TreeNode();
string _ChildNodes = dtleader.Rows[h][“name”].ToString();
// RootNode2.
}
}
}
//treeView1.Nodes[0].Expand();
TrV.ExpandAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
本人刷新的时候把上面那个方法又执行了一下,在TREEVIEW中出现了重复的节点,问一下该怎么样解决
解决方案
5
刷新之前先清空treeview
15
每次刷新前clear节点(TreeView1.Nodes.Clear),然后再添加节点。