|
Subject:
|
Treeview Control
|
|
Posted By:
|
pavank
|
Post Date:
|
5/21/2007 5:41:24 AM
|
hai friends ,
i am new member of this group, i have on problem in
treeview control i.e how to add parents and child node at the same time at btn click events using server database

pavan
|
|
Reply By:
|
mike_remember
|
Reply Date:
|
5/21/2007 9:11:45 AM
|
Well, be precise with your query, in general you can first create a parent node, add it to the main treeview control, and then add child nodes to the parent node as below:
trParentNode = new TreeNode("Parent","Parent"); trvMain.Nodes.Add(trParentNode );
trChildNode = new TreeNode("Child","Child"); trParentNode.ChildNodes.Add(trChildNode);
Regards Mike
Fortune favours the brave, so don't regret on missed oppurtunities.
|
|
Reply By:
|
hariharan.it
|
Reply Date:
|
5/25/2007 3:42:48 AM
|
using this code u can add,edit delete nodes at runtime
String constr = ConfigurationManager.ConnectionStrings["DbConn"].ToString(); NpgsqlDataAdapter da; NpgsqlConnection conn; NpgsqlCommand cmd; DataSet ds; DataTable dt; DataRow dr; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { PopulateRootLevel(); } conn = new NpgsqlConnection(constr); conn.Open(); }
public void PopulateRootLevel() { conn = new NpgsqlConnection(constr); conn.Open(); cmd = new NpgsqlCommand("select id,topic,(select count(*) FROM samtopic WHERE parentid=sc.id) as childnodecount FROM samtopic sc where parentID = 0", conn); da = new NpgsqlDataAdapter(cmd); dt = new DataTable(); da.Fill(dt); PopulateNodes(dt, TreeView1.Nodes); } public void PopulateNodes(DataTable dt,TreeNodeCollection nodes) { foreach(DataRow dr in dt.Rows) { TreeNode tn = new TreeNode(); tn.Text = dr["topic"].ToString(); tn.Value = dr["id"].ToString(); nodes.Add(tn); //If node has child nodes, then enable on-demand populating tn.PopulateOnDemand = (Convert.ToInt32(dr["childnodecount"]) > 0); } TreeView1.ExpandAll(); }
public void PopulateSubLevel(int parentid, TreeNode parentNode) { conn = new NpgsqlConnection(constr); conn.Open(); cmd = new NpgsqlCommand("select id,topic,(select count(*) FROM samtopic WHERE parentid=sc.id) as childnodecount FROM samtopic sc where parentid = @parentid", conn); cmd.Parameters.Add("@parentid", NpgsqlTypes.NpgsqlDbType.Numeric).Value = parentid; da = new NpgsqlDataAdapter(cmd); dt = new DataTable(); da.Fill(dt); PopulateNodes(dt, parentNode.ChildNodes); }
protected void TreeView1_TreeNodePopulate(object sender , System.Web.UI.WebControls.TreeNodeEventArgs e) { PopulateSubLevel(Convert.ToInt32(e.Node.Value), e.Node); }
public void instreeview() { if(TreeView1.CheckedNodes.Count == 0) { TreeNode tn = new TreeNode(); //int parid = Convert.ToInt32(node.Value); // string Text = txtnode.Text; cmd = new NpgsqlCommand("insert into samtopic(parentid,topic) values(0,'" + txtnode.Text + "')", conn); cmd.ExecuteNonQuery(); } else { foreach (TreeNode node in TreeView1.CheckedNodes) { TreeNode tn = new TreeNode(); int parid = Convert.ToInt32(node.Value); cmd = new NpgsqlCommand("insert into samtopic(parentid,topic) values(" + parid + ",'" + txtnode.Text + "')",conn); cmd.ExecuteNonQuery(); } } TreeView1.Nodes.Clear(); PopulateRootLevel();
} protected void btnadd_Click(object sender, EventArgs e) { instreeview(); } protected void btndel_Click(object sender, EventArgs e) { foreach (TreeNode node in TreeView1.CheckedNodes) {
TreeNode tn = new TreeNode(); int parid = Convert.ToInt32(node.Value); // string Text = txtnode.Text; cmd = new NpgsqlCommand("delete from samtopic where id = " + parid +"", conn); cmd.ExecuteNonQuery(); } TreeView1.Nodes.Clear(); PopulateRootLevel(); }
protected void btnedit_Click(object sender, EventArgs e) { edittreeview(); } public void edittreeview() { foreach (TreeNode node in TreeView1.CheckedNodes) {
TreeNode tn = new TreeNode(); int parid = Convert.ToInt32(node.Value);
cmd = new NpgsqlCommand("update samtopic set topic = '" + txtnode.Text + "' where id = " + parid + "", conn); cmd.ExecuteNonQuery(); } TreeView1.Nodes.Clear(); PopulateRootLevel(); } }
|