0

树视图加载了来自数据库的一些数据。我想过滤所选组合框项目的树视图。有人告诉我如何执行此操作吗?

 public SelectorDataTreeView()
        {            
            InitializeComponent();

            button1.FlatStyle = FlatStyle.Flat;

            comboBox1.Items.Add("HP");
            comboBox1.Items.Add("DELL");
            comboBox1.Items.Add("Cam");

        }






        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           String s = comboBox1.SelectedItem.ToString();
           ExpandAll(MyTreeView.Nodes);
        }




        //TreeKey Finder
        private TreeNode[] FindNode(String searchKey)
        {

            return MyTreeView.Nodes.Find(searchKey, true);

        }
}
4

2 回答 2

1
private void Form4_Load(object sender, EventArgs e)
    {
        treeView1.Nodes.Add("LaptopsNodeKey", "Laptop");
        treeView1.Nodes.Add("CellPhonesNodeKey", "Phones");
        treeView1.Nodes.Find("LaptopsNodeKey", true)[0].Nodes.Add("DellTM", "Dell");
        treeView1.Nodes.Find("LaptopsNodeKey", true)[0].Nodes.Add("SonyTM", "Sony");
        treeView1.Nodes.Find("CellPhonesNodeKey", true)[0].Nodes.Add("HTCTM", "HTC");
        treeView1.Nodes.Find("CellPhonesNodeKey", true)[0].Nodes.Add("NokiaTM", "Nokia");

        comboBox1.Items.Add("LaptopsNodeKey");
        comboBox1.Items.Add("CellPhonesNodeKey");

        comboBox2.Items.Add("Laptop");
        comboBox2.Items.Add("Phones");
    }

    //This works because you Loaded ComboBox1 with Name property of Nodes
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        treeView1.CollapseAll();
        treeView1.SelectedNode = treeView1.Nodes.Find(comboBox1.Text, true)[0];
        treeView1.SelectedNode.Expand();
    }

    //This doesn't work, because you Loaded ComboBox2 with Text property of Nodes
    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        treeView1.CollapseAll();
        treeView1.SelectedNode = treeView1.Nodes.Find(comboBox1.Text, true)[0];
        treeView1.SelectedNode.Expand();
    }
于 2012-07-01T11:51:50.290 回答
1

啊哈,这是你的错:
String s = comboBox1.SelectedItem.ToString();
你应该使用这个:
String s = comboBox1.Text;
它正在工作。
而且你应该搜索节点Name属性而不是Text属性。
得到后s你需要像这样使用它
treeView1.SelectedNode = treeView1.Nodes.Find("WantedNodesName", true)[0]; treeView1.SelectedNode.Expand();

于 2012-07-01T12:05:23.757 回答