/// <summary> /// 根据ParentId返回对应的版块的集合方法,集合元素是保存版块信息的Category对象 方法 /// </summary> /// <param name="pid">parentid</param> /// <returns></returns> private List<Category> GetCategoryByParentId(int pid) { List<Category> list = new List<Category>(); string sql = "select tId,tName from category where tParentId=@pid"; using (SqlDataReader reader = SqlHelper.ExecuteSqlDataReader(sql, new SqlParameter("@pid", pid))) { if (reader.HasRows) { while (reader.Read()) { Category c = new Category(); c.tId = reader.GetInt32(0); c.tName = reader.GetString(1); list.Add(c); } } } return list; }
/// <summary> /// treeView子节 菜单点击添加子类别事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 增加子类ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.ShowDialog(); Category category = new Category(); TreeNode node = treeView1.SelectedNode.Nodes.Add(f2.name); node.ContextMenuStrip = contextMenuStrip1;//给新节点绑定右键菜单 //给TreeView增加新类别完成 //但记得把tid放到新的节点的tag中,而tid是Category表中的主键,要在表中增加这条数据才能获得 string sql = "insert into Category(tName,tParentId) output inserted.tId values(@name,@parentId)"; SqlParameter[] parameters = new SqlParameter[]{ new SqlParameter("@name",f2.name), new SqlParameter("@parentId",(int)treeView1.SelectedNode.Tag) }; int tid = (int)SqlHelper.ExecuteScalar(sql, parameters); node.Tag = tid; }