private void toolStripButtonDelete_Click(object sender, EventArgs e) { if (trvwRsses.SelectedNode.Level == 2) //删除RSS节点 { if (DialogResult.Yes == MessageBox.Show("真的要删除选中的RSS来源吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)) { RssDAL.Delete(trvwRsses.SelectedNode.Name); BindTreeView(); } } else if (trvwRsses.SelectedNode.Level == 1) //删除分类节点 { string categoryID = trvwRsses.SelectedNode.Name; if (DialogResult.Yes == MessageBox.Show("真的要删除选中的分类,包括分类下所有的RSS来源吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)) { //删除分类下的所有 RSS 来源 foreach ( var r in RssDAL.GetAllRsses().Where(r => r.Category.ToString() == categoryID)) { RssDAL.Delete(r.ID.ToString()); } //删除分类本身 CategoryDAL.Delete(categoryID); BindTreeView(); } } }
private void BindTreeView() { trvwRsses.Nodes.Clear(); //增加根节点(全部分类) TreeNode rootNode = new TreeNode("所有分类", 0, 0) { Name = "/" }; trvwRsses.Nodes.Add(rootNode); //增加所有分类 foreach (var category in CategoryDAL.GetAllCategories()) { rootNode.Nodes.Add(new TreeNode(category.CategoryName, 1, 1) { Name = category.ID.ToString() }); } var rsses = RssDAL.GetAllRsses(); //增加所有 RSS 节点 foreach (TreeNode node in trvwRsses.Nodes["/"].Nodes) { foreach (var rss in rsses.Where(r => r.Category.ToString() == node.Name)) { node.Nodes.Add(new TreeNode(rss.Title, 2, 2) { Name = rss.ID.ToString(), ToolTipText = rss.Description }); } } trvwRsses.ExpandAll(); }