【问题标题】:Tree view filtering does not return all the files树视图过滤不返回所有文件
【发布时间】:2012-11-25 15:21:28
【问题描述】:

我的树视图由文件夹和文件填充。我已经使用下面的代码来过滤树视图,但它不会返回所有匹配字符串的文件,它只返回一个文件。 例如字母“f”在 3 个文件中,但当我搜索时它只返回 1 个文件。

private TreeNode FindNodeByValue(TreeNodeCollection nodes, string searchstring)
{

    // Loop through the tree node collection
    foreach (TreeNode node in nodes)
    {
        // Does the value match the search string?
        if (node.Value.ToUpper().Contains (searchstring.ToUpper()))
            // Yes it does match - return it
            return node;
        else
        {
            // No it does not match - search any child nodes of this node
            TreeNode childNode = SearchChildNodes(node, searchstring);
            // If the childNode is not null it was a match
            if (childNode != null)
                // Return the matching node
                return childNode;
        }
    }
    // If the matching node is not found return null
    return null;
}

/// <summary>
/// This method searches a node's ChildNodes collection to find a matching value
/// with the incoming search string
/// It will iteratively call itself as it drills into each nodes child nodes (if present)
/// </summary>
/// <param name="parentNode">Parent node to search for a match</param>
/// <param name="searchstring">string to be matched with the Nodes Value property</param>
/// <returns>Treenode of the matching node if found.  If not found it will be null</returns>
private TreeNode SearchChildNodes(TreeNode parentNode, string searchstring)
{
    // Loop through the child nodes of the parentNode passed in
    foreach (TreeNode node in parentNode.ChildNodes)
    {
        // Does the value match the search string?
        if (node.Value.ToUpper().Contains(searchstring.ToUpper()))
            // Yes it does match - return it
            return node;
        else
        {
            // No it does not match - recursively search any child nodes of this node
            TreeNode childNode = SearchChildNodes(node, searchstring);
            // If the childNode is not null it was a match
            if (childNode != null)
                // Return the matching node
                return childNode;
        }
    }
    // If the matching node is not found OR if there were no child nodes then return null
    return null;
}


protected void Button1_Click(object sender, EventArgs e)
{
    TreeNode trnode=FindNodeByValue(TreeView1.Nodes, fieldFilterTxtBx.Text);
    if (trnode != null)
    {
        TreeView1.Nodes.Clear();
      //  TreeNode newnode = new TreeNode("Detail Engineering");
       // TreeView1.Nodes.Add(newnode);
        TreeView1.Nodes.Add(trnode);
        TreeView1.ExpandAll();
    }
    else

{

Label1.Text = "No file found";

}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    它只返回一个值,因为如果没有找到文件,该函数只会递归调用自身。您还需要将递归添加到 SearchChildNodes 中 if 语句的此分支:

    if (node.Value.ToUpper().Contains(searchstring.ToUpper()))
    // Yes it does match - return it
        return node;
    else
    

    您可能还需要将文件名添加到数组或 Generic.List 以便一次存储多个文件。

    【讨论】:

    • 谢谢,只找到一个文件,是否可以发送有关如何在数组或通用列表中存储多个文件的完整代码
    • 我不会发送完整的代码,但您可以在调用“FindNodeByValue”的地方发送。您可以像创建任何其他变量一样创建 generic.list,然后使用实例方法“.Add()”添加每个值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多