Introduction

This article explains how to create a tree view, or we can say a hierarchical view, structure. We can also do this using a HTML Table but I have a better option from the Google. So this article explains how to execute Google logic to generate a hierarchical view where the data will come from the database.

Before proceeding further just have a look at the URL Visualization: Organizational Chart. This link will help you to understand how to implement our hierarchical view.
Let's create a project to understand how to use Visualization: Organizational Chart.
Before starting the project, create the database first. I am creating a database and in which I am creating 2 tables, DeptMaster and EmpMaster, such as in the following:
DeptMaster Table



EmpMaster Table

In the DeptMaster table I have the following departments that were inserted.

In the EmpMaster table I have the following employees that were inserted.

Now create an ASP.NET project and add a webform. Now in the webform create a div with a id (tree_div) and include the CDN of the Google API. You can use the following code:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="HierarchicalViewInASP.Home" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['orgchart']}]}"></script>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <div id="tree_div"">
  12. </div>
  13. </div>
  14. </form>
  15. </body>
  16. </html>
And at the code behind use the following code:
  1. private void BindData()
  2. {
  3. string str="";
  4. string qry = @"select DepartmentName from DeptMaster;select E.Emp_Name,D.DepartmentName from DeptMaster D
  5. inner join EmpMaster E on E.Dept_ID=D.Dept_id";
  6. SqlConnection con = new SqlConnection(@"server=.\SQLExpress;DATABASE=HierarchicalRelation;integrated security=true;");
  7. SqlDataAdapter da = new SqlDataAdapter(qry, con);
  8. DataSet ds = new DataSet();
  9. da.Fill(ds);
  10. foreach (DataRow dr in ds.Tables[0].Rows)
  11. {
  12. str = str + "['" + dr["DepartmentName"].ToString() + "','University'],";
  13. }
  14. foreach (DataRow dr in ds.Tables[1].Rows)
  15. {
  16. str = str + "['" + dr["Emp_Name"].ToString() + "','" + dr["DepartmentName"].ToString() + "'],";
  17. }
  18. str.Trim(',');
  19. String csname1 = "PopupScript";
  20. ClientScriptManager cs = Page.ClientScript;
  21. if (!cs.IsStartupScriptRegistered(typeof(Button), csname1))
  22. {
  23. StringBuilder scriptText = new StringBuilder();
  24. scriptText.Append("<script>");
  25. scriptText.Append("google.setOnLoadCallback(drawChart);");
  26. scriptText.Append("function drawChart() {");
  27. scriptText.Append("var data = new google.visualization.DataTable();");
  28. scriptText.Append("data.addColumn('string', 'Name'); data.addColumn('string', 'Manager');");
  29. scriptText.Append("data.addRows([" + str + "]);");
  30. scriptText.Append("var chart = new google.visualization.OrgChart(document.getElementById('tree_div'));");
  31. scriptText.Append("chart.draw(data, { allowHtml: true });");
  32. scriptText.Append("}");
  33. scriptText.Append("</script>");
  34. cs.RegisterStartupScript(typeof(Button), csname1, scriptText.ToString());
  35. }
  36. }
Output


Happy Coding :)