This article shows how to show our records in a tree view using ASP.NET C#.

The following is my Data table design.

design view
Image 1

The following are the records in my Data Table:

table record
Image 2

Here I have Employees and their SupervisorEmployee relationship.

The following is my aspx code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Tree View Example in ASP.NET C#</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <table cellpadding="4" cellspacing="4" width="50%" align="center" style="background-color: SkyBlue;">
  10. <tr>
  11. <td align="center" style="font-family: Times New Roman; font-size: 14pt; color: Green;">
  12. <h3>
  13. Team Leader and their Team Employee Information</h3>
  14. </td>
  15. </tr>
  16. <tr>
  17. <td style="background: #F5F5F5; padding-left: 30px;">
  18. <asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
  19. <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
  20. <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
  21. NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
  22. <ParentNodeStyle Font-Bold="False" />
  23. <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
  24. VerticalPadding="0px" />
  25. </asp:TreeView>
  26. </td>
  27. </tr>
  28. </table>
  29. </form>
  30. </body>
  31. </html>
Now, my aspx.cs code:
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Data.SqlClient;
  13. using System.Configuration;
  14. public partial class _Default : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. if (!this.IsPostBack)
  19. {
  20. DataTable dt = this.GetData("SELECT EmployeeCode, EmployeeName FROM Employee Where EmployeeSupervisorCode=0");
  21. this.PopulateTreeView(dt, 0, null);
  22. }
  23. }
  24. private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
  25. {
  26. foreach (DataRow row in dtParent.Rows)
  27. {
  28. TreeNode child = new TreeNode
  29. {
  30. Text = row["EmployeeName"].ToString(),
  31. Value = row["EmployeeCode"].ToString()
  32. };
  33. if (parentId == 0)
  34. {
  35. TreeView1.Nodes.Add(child);
  36. DataTable dtChild = this.GetData("SELECT EmployeeCode, (EmployeeName+ ', '+ Experience + ', '+ Address) As EmployeeName FROM Employee WHERE EmployeeSupervisorCode = " + child.Value);
  37. PopulateTreeView(dtChild, int.Parse(child.Value), child);
  38. }
  39. else
  40. {
  41. treeNode.ChildNodes.Add(child);
  42. }
  43. }
  44. }
  45. private DataTable GetData(string query)
  46. {
  47. DataTable dt = new DataTable();
  48. string constr = @"Server=MyPC\SqlServer2k8;database=Test;Integrated Security=true;";
  49. using (SqlConnection con = new SqlConnection(constr))
  50. {
  51. using (SqlCommand cmd = new SqlCommand(query))
  52. {
  53. using (SqlDataAdapter sda = new SqlDataAdapter())
  54. {
  55. cmd.CommandType = CommandType.Text;
  56. cmd.Connection = con;
  57. sda.SelectCommand = cmd;
  58. sda.Fill(dt);
  59. }
  60. }
  61. return dt;
  62. }
  63. }
  64. }
Now, run the application:

tree view example
Image 3