C# 我想在列表中添加一个对象,但嵌套循环的组织方式使这不可能(C)
所以我正在制作一个程序,它有几个类别,其中存储了多个注释。我是一名学生,我们被指派与SQLServer的localdb一起保存我们的类别和注释。程序使用treeview控件显示类别和注释 现在我正试图为我的笔记制作一个搜索栏。它必须根据注释的标题进行过滤 我的问题是,如何从一个类别中获取所有注释并将它们添加到同一个类别中?现在,所有注释都以不同类别显示,但名称相同。这是因为我的主循环处理注释,而内循环处理类别 当我放置categoriesUI1.Categories.Addcategory时;在while循环后面,我只看到一个类别和一个注释,因为注释的while循环首先被处理 以下是一些额外的屏幕截图,让您更好地了解我的程序的工作原理: 筛选前: 在“test2”上进行筛选后: 我给了你我的idae 1.你通过搜索键获得所有美食。 2.获取并添加通过searchKey和Cat_id归档的所有注释,并将其添加到适当的类别中 细节如下,只是一些想法,尚未测试。希望能有所帮助C# 我想在列表中添加一个对象,但嵌套循环的组织方式使这不可能(C),c#,loops,nested,treeview,nodes,C#,Loops,Nested,Treeview,Nodes,所以我正在制作一个程序,它有几个类别,其中存储了多个注释。我是一名学生,我们被指派与SQLServer的localdb一起保存我们的类别和注释。程序使用treeview控件显示类别和注释 现在我正试图为我的笔记制作一个搜索栏。它必须根据注释的标题进行过滤 我的问题是,如何从一个类别中获取所有注释并将它们添加到同一个类别中?现在,所有注释都以不同类别显示,但名称相同。这是因为我的主循环处理注释,而内循环处理类别 当我放置categoriesUI1.Categories.Addcategory时;在
/// <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();
}
我修复了这个问题,只在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);
}
}
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);
}
}