Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 21st, 2007, 05:41 AM
Registered User
 
Join Date: May 2007
Location: guntur, ap, India.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to pavank
Default Treeview Control

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
  #2 (permalink)  
Old May 21st, 2007, 09:11 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
  #3 (permalink)  
Old May 25th, 2007, 03:42 AM
Registered User
 
Join Date: May 2007
Location: Chennai, Tamil Nadu, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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();
    }
}




Similar Threads
Thread Thread Starter Forum Replies Last Post
Treeview Control cymerman ASP.NET 1.0 and 1.1 Basics 0 March 26th, 2008 08:57 AM
TreeView Control ugarcia BOOK: Expert One-on-One Access Application Development 0 April 20th, 2007 01:57 PM
how to use treeview control yogesh21 .NET Framework 2.0 0 September 7th, 2005 04:56 AM
Treeview Control Casanova1 VB.NET 2002/2003 Basics 0 January 8th, 2004 04:27 PM
TreeView Control bmains VB.NET 2002/2003 Basics 1 July 10th, 2003 05:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.