How To show data from database into treeview

Posted by Bhupender.Yadav under ASP.NET on 4/17/2013 | Points: 10 | Views : 6905 | Status : [Member] | Replies : 4
hey guys
I Am working on a project in which i have to table
1. Project
2. Subproject
and i want to save then in a treeview .in which the parent node in the tree view will be Project and the child node will be SubProject.
but i am not able to understand how to do this. i am using tree view first time.please help me out of this.

With Regards,
Bhupender Yadav



Responses

Posted by: Satyapriyanayak on: 4/17/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Table1: Category - Columns are Category_ID, CategoryName
Table2: SubCategory -Columns are SubCategory_ID, SubCategoryName, Category_ID


Category Table Data:

1 Item1
2 Item2
3 Item3
4 Item4

SubCategory Table Data

1 SubItem1 1
2 SubItem2 1
3 SubItem3 1
4 SubItem4 2
5 SubItem5 2
6 SubItem6 3
7 Subitem7 3
8 Subitem8 4



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TreeView_in_ASP.NET._Default" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:treeview ID="Treeview1" runat="server"></asp:treeview>
</div>
</form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace TreeView_in_ASP.NET
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com, com1;

protected void Page_Load(object sender, EventArgs e)
{
bindtreeview();
}
void bindtreeview()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "Select * from Category";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
com.Dispose();
string[,] ParentNode = new string[100, 2];
int count = 0;
while (reader.Read())
{
ParentNode[count, 0] = reader.GetValue(reader.GetOrdinal("Category_ID")).ToString();
ParentNode[count++, 1] = reader.GetValue(reader.GetOrdinal("CategoryName")).ToString();
}
reader.Close();
for (int loop = 0; loop < count; loop++)
{
TreeNode root = new TreeNode();
root.Text = ParentNode[loop, 1];
com1 = new SqlCommand("Select * from SubCategory where Category_ID =" + ParentNode[loop, 0], con);
SqlDataReader reader1 = com1.ExecuteReader();
while (reader1.Read())
{
TreeNode child = new TreeNode();
child.Text = reader1.GetValue(reader1.GetOrdinal("SubCategoryName")).ToString();
root.ChildNodes.Add(child);
}
reader1.Close();
Treeview1.Nodes.Add(root);
}
Treeview1.CollapseAll();
con.Close();
}
}
}


If this post helps you mark it as answer
Thanks

If this post helps you mark it as answer
Thanks

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 4/17/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Bhupender

Check this



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.Configuration;
using System.Data.SqlClient;

public partial class TreeView : System.Web.UI.Page
{
string strconn = ConfigurationManager.ConnectionStrings["cnnLocal"].ToString();
SqlConnection sqlconn;
DataSet dstreenode = new DataSet();

//string CategoryID;
TreeNode root = new TreeNode();
TreeNode xnode = new TreeNode();


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRoot();
}
}

private void BindRoot()
{
sqlconn = new SqlConnection(strconn);
sqlconn.Open();
DataTable dt = RootNodes();
TreeNode headnode = new TreeNode();
headnode.Text = "Root";
//headnode.Value = dt.Rows[0]["CategoryID"].ToString();
xtreeCat.Nodes.Add(headnode);
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeNode firstchild = new TreeNode();
firstchild.Text = dt.Rows[i]["CategoryName"].ToString();
firstchild.Value = dt.Rows[i]["CategoryID"].ToString();
headnode.ChildNodes.Add(firstchild);
DataTable dt1 = ChildNodes(Convert.ToString(firstchild.Value));
for (int j = 0; j < dt1.Rows.Count; j++)
{
TreeNode childnode = new TreeNode();
childnode.Text = dt1.Rows[j]["ImageName"].ToString();
childnode.Value = dt1.Rows[j]["CategoryImageID"].ToString();
firstchild.ChildNodes.Add(childnode);
}

}
}

private DataTable RootNodes()

{
sqlconn = new SqlConnection(strconn);
sqlconn.Open();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("select * from CategoriesMaster",sqlconn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}


private DataTable ChildNodes(string ParentNode)
{

sqlconn = new SqlConnection(strconn);
sqlconn.Open();
SqlCommand cmd = new SqlCommand("select * from CategoriesImageMaster where CategoryID = '" + ParentNode + "'",sqlconn);
SqlDataAdapter da1= new SqlDataAdapter();
DataTable dt = new DataTable();
da1.SelectCommand = cmd;
da1.Fill(dt);
return dt;
}



}









Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupender.Yadav on: 4/18/2013 [Member] Starter | Points: 25

Up
0
Down
Hello Raj & SatyaPriya,

Thanks For ur respond. its very helpfull to me. but can u tell me how to use treeview with entityframwork.

With Regards,
Bhupender Yadav

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 4/18/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Refer this link:
u will get an idea.
http://www.codeproject.com/Articles/14799/Populating-a-TreeView-Control-from-the-Database

Thanks & Regards,
Santhi .V

Bhupender.Yadav, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response