Hi malobaili,
Please refer below articles
http://www.aspforums.net/Threads/110940/How-to-bind-TreeView-control-from-database-in-ASPNet/
Here I have created sample.
HTML
<div>
<h3>
User Details</h3>
<hr />
<asp:TreeView ID="TreeView1" runat="server" ImageSet="News" NodeIndent="15">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
</div>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData("SELECT Ma_Phong, TenPhong FROM TB_PhongBan");
this.PopulateTreeView(dt, string.Empty, null);
}
}
private void PopulateTreeView(DataTable dtParent, string parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row[1].ToString(),
Value = row[0].ToString()
};
if (string.IsNullOrEmpty(parentId))
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT MaPhong, HoVaTen FROM TB_User WHERE MaPhong ='" + child.Value + "'");
PopulateTreeView(dtChild, 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;
}
}
Screenshot
I hope this will help you out.