我想在我的列表中添加一个对象,但嵌套循环的组织方式使得这不可能(C#)

时间:2016-01-03 02:57:18

标签: c# loops nested treeview nodes

所以我正在制作一个程序,它有几个类别,里面存有多个音符。我是一名学生,我们获得了与SQL Server的localdb一起工作的任务,以保存我们的类别和注释。该程序使用树视图控件显示类别和注释。

现在我正在尝试为我的笔记创建一个搜索栏。它必须过滤笔记的标题。

我的问题是,如何从类别中获取所有笔记并将其添加到同一类别?现在所有注释都以不同的类别显示,但名称相同。这是因为我的主循环处理注释,而内循环处理类别。

当我把“categoriesUI1.Categories.Add(category);”在while循环后面我只看到1个带有1个音符的类别,因为音符的while循环首先被处理。

以下是一些额外的屏幕截图,可让您更好地了解我的计划如何运作:

过滤前: Mix_LoadWAV_RW

过滤'test2'后: pygame docs

null

2 个答案:

答案 0 :(得分:0)

我给了你我的idae

1.你通过Searchkey获得所有Cates。 2.get添加由searchKey和Cat_id压缩的所有Notes,将它们添加到正确的Cate

详细信息如下,只是一些想法,尚未经过测试。希望它有所帮助。

    /// <summary>
    /// initail your  UI
    /// </summary>
    void ini()
    {


        connection = new SqlConnection(@"Data Source=(localdb)\mssqllocaldb;Initial Catalog=dbNotes;Integrated Security=True;MultipleActiveResultSets=true");
        categoriesUI1.tvwCategories.SelectedNode = categoriesUI1.tvwCategories.TopNode;
        int count = categoriesUI1.Categories.Count;

        if (count > 0)
        {
            for (int i = count; i > 0; i = count)
            {
                categoriesUI1.Categories.RemoveAt(0);
                count = categoriesUI1.Categories.Count;
            }
        }

    }







    /// <summary>
    /// this is for add all searchressult Cates to to Treeview
    /// </summary>
    /// <param name="sKey"></param>
    void SearchCate(string sKey)
    {
           connection.Open();
        SqlCommand commandCategory = new SqlCommand("SELECT Titel FROM tblCategory WHERE ID in ( 
SELECT ID  FROM tblNote WHERE Titel LIKE '%" + sKey +
                                "%') " + Cat_id, connection);


        SqlDataReader readerCategory = commandCategory.ExecuteReader();
        if (readerCategory.HasRows)
        {
            while (readerCategory.Read())
            {
                Cat_title = readerCategory.GetString(0);
                category = new Category(Cat_id, Cat_title); // new category
                categoriesUI1.Categories.Add(category);

                // then add your note's search result here to eavcgorhe

                AddNoteToACateBySearchKey(sKey,category);

            }
        }



    }



    /// <summary>
    /// add all searchresult note to cate 
    /// </summary>
    /// <param name="sKey"></param>
    /// <param name="Parent"></param>

    void AddNoteToACateBySearchKey(string sKey, Category Parent )
    {


        connection.Open();
        SqlCommand commandNote = new SqlCommand("SELECT ID, Cat_ID, Titel, Text, Created FROM tblNote WHERE Titel LIKE '%" + sKey + "%' and Cat_ID='" + Parent.Cat_id + "' ", connection);
        SqlDataReader readerNote = commandNote.ExecuteReader();
        if (readerNote.HasRows)
        {
            Category category = new Category();
            while (readerNote.Read())
            {
                int id = readerNote.GetInt32(0);
                int Cat_id = readerNote.GetInt32(1);
                string title = readerNote.GetString(2);
                string text = readerNote.GetString(3);
                DateTime created = readerNote.GetDateTime(4);
                string Cat_title = "";



                Note note;
                note = new Note(id, title, text, created); // new note
                category.Notes.Add(note);
                readerCategory.Close();




            }



        }

        readerNote.Close();
        connection.Close();

    }

答案 1 :(得分:0)

仅当Cat_ID与当前类别的ID相对应时,才通过添加注释来修复它。现在主循环处理类别,内循环处理笔记。

connection = new SqlConnection(@"Data Source=(localdb)\mssqllocaldb;Initial Catalog=dbNotes;Integrated Security=True;MultipleActiveResultSets=true");
        categoriesUI1.tvwCategories.SelectedNode = categoriesUI1.tvwCategories.TopNode;
        int count = categoriesUI1.Categories.Count;

        if (count > 0)
        {
            for (int i = count; i > 0; i = count)
            {
                categoriesUI1.Categories.RemoveAt(0);
                count = categoriesUI1.Categories.Count;
            }
        }

        connection.Open();
        SqlCommand commandCategory = new SqlCommand("SELECT ID, Titel from tblCategory", connection);
        SqlDataReader readerCategory = commandCategory.ExecuteReader();
        if (readerCategory.HasRows)
        {
            while (readerCategory.Read())
            {
                int id = readerCategory.GetInt32(0);
                string titel = readerCategory.GetString(1);
                Category category = new Category(id, titel);

                SqlCommand commandNote = new SqlCommand("SELECT ID, Cat_ID, Titel, Text, Created FROM tblNote WHERE Titel LIKE '%" + searchBarUI1.getTextValue() + "%'", connection);
                SqlDataReader readerNote = commandNote.ExecuteReader();
                if (readerNote.HasRows)
                {
                    while (readerNote.Read())
                    {
                        int catID = readerNote.GetInt32(1);
                        if (catID == id)
                        {
                            int noteID = readerNote.GetInt32(0);
                            string noteTitel = readerNote.GetString(2);
                            string noteText = readerNote.GetString(3);
                            DateTime noteCreated = readerNote.GetDateTime(4);
                            Note note = new Note(noteID, noteTitel, noteText, noteCreated);
                            category.Notes.Add(note);
                        }
                    }
                }
                if(category.Notes.Count > 0)
                    categoriesUI1.Categories.Add(category);
            }
        }