Pages

Monday, January 9, 2012

Bind Treeview in asp.net c# using dataset

Bind Treeview in asp.net c# using dataset
Introduction : In this article i will show you how to bind treeview in c# using dataset . I have created complete code to bind the treeview and also table structure in image which require for the treeview control to bind in asp.net c# using dataset .
Table structure :
HTML code treeview in asp.net :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeViewControl.aspx.cs"
    Inherits="HamidSite.TreeViewControl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="treeviwExample"  runat="server" ImageSet="Arrows">
            <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
            <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />            
        </asp:TreeView>
    </div>
    </form>
</body>
</html>

C# Code treeview control in asp.net :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace HamidSite
{
    public partial class TreeViewControl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                BindTreeViewControl();
            }
        }
        private void BindTreeViewControl()
        {
            try
            {
                DataSet ds = GetDataSet("Select ProductId,ProductName,ParentId from ProductTable");
                DataRow[] Rows = ds.Tables[0].Select("ParentId IS NULL"); // Get all parents nodes
                for (int i = 0; i < Rows.Length; i++)
                {
                    TreeNode root = new TreeNode(Rows[i]["ProductName"].ToString(), Rows[i]["ProductId"].ToString());
                    root.SelectAction = TreeNodeSelectAction.Expand;
                    CreateNode(root, ds.Tables[0]);
                    treeviwExample.Nodes.Add(root);
                }
            } catch (Exception Ex) { throw Ex; }
        }

        public void CreateNode(TreeNode node , DataTable Dt)
        {
            DataRow[] Rows = Dt.Select("ParentId =" + node.Value);            
            if (Rows.Length == 0) { return; }
            for (int i = 0; i < Rows.Length; i++)
            {
                TreeNode Childnode = new TreeNode(Rows[i]["ProductName"].ToString(), Rows[i]["ProductId"].ToString());
                Childnode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(Childnode);
                CreateNode(Childnode, Dt);
            }
        }
        private DataSet GetDataSet(string Query)
        {           
            DataSet Ds = new DataSet();
            try {
                string strCon = @"Data Source=Servername;Initial Catalog=Test;Integrated Security=True"; 
                SqlConnection Con = new SqlConnection(strCon);
                SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
                Da.Fill(Ds);
            } catch (Exception) { }
            return Ds; 
        }
    }
}

For Check / Uncheck Checkboxes in treeview control using jquery you can visit following post :- Check / Uncheck Checkboxes in treeview control using jquery

Related Other posts

35 comments:

  1. Hi All,

    I have a table as same with this article and I have a table another with ProductId as Foreign Key also as ParentId. Example:
    Table A
    Id(PK) | ParentId | Name
    1 0 Apple
    2 0 Dell
    3 0 Asus
    4 1 Apple 001
    5 1 Apple 002
    6 2 Dell 001
    7 2 Dell 002
    8 3 Asus 001
    9 3 Asus 002

    Table B
    Id(PK) | AId(FK) | Color
    1 4 Bule
    2 5 Black
    3 6 White
    4 7 Red
    5 8 Pink
    6 9 Yellow

    How can I Bind Treeview in asp.net c# using dataset?Thanks!

    ReplyDelete
  2. how to delete nodes from treeview on a button click bind to dataset??
    plzz can anyone help

    ReplyDelete
  3. i have used code from this example

    ReplyDelete
  4. Hi,
    Well done,
    How do i change all parents check state when a child is selected(enable three state),
    like this http://www.jeasyui.com/tutorial/tree/tree4.php
    can anyone help me?
    thanks in advance.

    ReplyDelete
  5. Hey,really excellent article,it worked out, thanks a lot, more than 2 weeks i was struggling witha tree view , my problem got solved, keep it up, DUDE
    and to pass the values in #SelectedNodeChanged event
    just add this line:Childnode.SelectAction = TreeNodeSelectAction.Select;
    instead of expand give select;

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. Thank u a lot..its very useful..

    ReplyDelete
  9. hi,
    thx a LoT
    its working smooth and fine

    ReplyDelete
  10. thank you so much, your code is working now ^_^

    ReplyDelete
  11. Hi,

    This was very help full to me.

    Thank you :)

    ReplyDelete
  12. GOOD LOGIC FOR THE DEVELOPERS WHO WANT TO IMPLEMENT THIS STRATEGY :)

    ReplyDelete
  13. is there any possibility that whn node get clicked we get the messagebox

    ReplyDelete
  14. its really very urgent i have used this code but i get stucked in node event fired

    ReplyDelete
  15. Thanx bro.... Please continue your posting...

    ReplyDelete
  16. thanks buddy..
    its really great...

    can anyone tell me that how can i get all childs nodes for particular parent....
    its urgent........

    ReplyDelete
  17. can anyone tell me that how can i get all childs nodes for particular parent....
    its urgent........

    ReplyDelete
  18. I never expected such a fantastic response to this query. Your solution is far superior to mine--just what I was looking for.


    Packers and Movers Pondicherry

    ReplyDelete
  19. Kabera Global hair transplant is providing
    The Best Hair Transplant in Delhi at best cost available in the Capital city. We have top hair transplant surgeons in our panel in Delhi.

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete
  21. Hair Transplant In Chandigarh Kabera global provide Hair Transplant service in Chandigarh at Affordable price.It gives treatment nearby your location. It provide best solution according to your budget and requirement.

    ReplyDelete
  22. Love it , it helped me a lot, thanks

    ReplyDelete
  23. Where we find that control HamidSite.TreeViewControl

    ReplyDelete
  24. Hair transplant in pune It is a good article post.Thanks for sharing.

    ReplyDelete
  25. Buy the different sizes of iv cannula like 14g to 26g from mais india, For more details visit our website -https://maisindia.com/ivcannula
    Mais India | IV Cannula | Safety IV Cannula

    ReplyDelete