TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Cao Cu Hao
NA
22
5.8k
Load database to treeview
Aug 5 2015 9:20 PM
I have the following data:
This is my code:
[code]
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = this.GetData("SELECT MaPhong, TenPhong FROM TB_PhongBan");
this.PopulateTreeView(dt, 0, null);
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["TenPhong"].ToString(),
Value = row["MaPhong"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT MaPhong, HoVaTen FROM TB_User WHERE MaPhong = " + child.Value);
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
[/code]
Relations between the two tables with the data type 'string' to error
:
This is table parent node:
This is table child node:
This is database exam:
http://4share.vn/f/2c181f1a1b1c151f/TreeView.bak
I want load database to treeview (user follow room). Help me!Thank
Reply
Answers (
6
)
How to Create New google sheet from asp.net and write data
Use Css Class in aspx/asp.net code