主题:treeview问题
为了实现树形,如通讯录,一级节点有,亲人,朋友,同事;二级节点:亲人下有1,朋友有2,同事下有3
代码如下:
myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Groups WHERE 用户名='" + currentUser + "'";
// 初始化myDataSet
myDataSet.Clear();
// 导入Groups表
myDataAdapter.Fill(myDataSet, "Groups");
DataTable dt = myDataSet.Tables["Groups"];
// 初始化TreeView的分组
TreeNode firstNode, secondNode;
foreach (DataRow dr in dt.Rows)
{
firstNode = new TreeNode();
firstNode.Text = (string)dr["组名"];
string groupname=(string)dr["组名"];
rootNode.Nodes.Add(firstNode);
myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Records WHERE 用户名='" + currentUser + "' and 组名='" + groupname + "'";
MessageBox.Show(groupname);
// 导入Records表
myDataAdapter.Fill(myDataSet, "Records");
DataTable dt1 = myDataSet.Tables["Records"];
foreach (DataRow dr1 in dt1.Rows)
{
secondNode = new TreeNode();
secondNode.Text = (string)dr1["姓名"];
firstNode.Nodes.Add(secondNode);
}
}
结果出现了
亲人下有1,朋友有1,2,同事下有1,2,3
问题就是:之前搜索出来的,在后面就会复制出来,怎么样才取消它啊??
代码如下:
myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Groups WHERE 用户名='" + currentUser + "'";
// 初始化myDataSet
myDataSet.Clear();
// 导入Groups表
myDataAdapter.Fill(myDataSet, "Groups");
DataTable dt = myDataSet.Tables["Groups"];
// 初始化TreeView的分组
TreeNode firstNode, secondNode;
foreach (DataRow dr in dt.Rows)
{
firstNode = new TreeNode();
firstNode.Text = (string)dr["组名"];
string groupname=(string)dr["组名"];
rootNode.Nodes.Add(firstNode);
myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Records WHERE 用户名='" + currentUser + "' and 组名='" + groupname + "'";
MessageBox.Show(groupname);
// 导入Records表
myDataAdapter.Fill(myDataSet, "Records");
DataTable dt1 = myDataSet.Tables["Records"];
foreach (DataRow dr1 in dt1.Rows)
{
secondNode = new TreeNode();
secondNode.Text = (string)dr1["姓名"];
firstNode.Nodes.Add(secondNode);
}
}
结果出现了
亲人下有1,朋友有1,2,同事下有1,2,3
问题就是:之前搜索出来的,在后面就会复制出来,怎么样才取消它啊??