1

I want to display a tree view from database. I did using asp.net TreeView controller. But i cannot afford TreeView controller on my project. Hence i am trying to create tree view dynamically.

Code

  using (SqlConnection Conn = new SqlConnection(connection))
                {
                    string State = "Select * from IN_State";
                    string City = "Select * from IN_City";

                    string Treeview = State + ";" + City;

                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(Treeview, Conn);
                    da.Fill(ds);
                    ds.Tables[0].TableName = "IN_State";
                    ds.Tables[1].TableName = "IN_City";

                    DataRelation dr = new DataRelation("StateCity", ds.Tables["IN_State"].Columns["S_Id"], ds.Tables["IN_City"].Columns["S_Id"]);
                    ds.Relations.Add(dr);

                    foreach (DataRow drState in ds.Tables["IN_State"].Rows)
                    {
                        TreeNode NDState = new TreeNode();
                        NDState.Text = drState["S_Name"].ToString();
                        NDState.Value = drState["S_Id"].ToString();
                        tview.Nodes.Add(NDState);

                        foreach (DataRow drCity in drState.GetChildRows("StateCity"))
                        {
                            TreeNode NDCity = new TreeNode();
                            NDCity.Text = drCity["C_Name"].ToString();
                            NDCity.Value = drCity["C_Id"].ToString();
                            NDState.ChildNodes.Add(NDCity);
                        }
                    }
                }

Also tried using System.Web.UI.WebControls.TreeView

2
  • Is your tree has only one level ( state => city)? In that case, it's more a simple master details view. Tree is overkill Commented Apr 11, 2019 at 15:07
  • 1
    will you please explain @maxence51 Commented Apr 11, 2019 at 15:08

2 Answers 2

1

I think, You are looking for TreeView constructor

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // Create a new TreeView control.
    TreeView NewTree = new TreeView();

    // Set the properties of the TreeView control.
    NewTree.ID = "BookTreeView";
    NewTree.DataSourceID = "BookXmlDataSource";

    // Create the tree node binding relationship.

    // Create the root node binding.
    TreeNodeBinding RootBinding = new TreeNodeBinding();
    RootBinding.DataMember = "Book";
    RootBinding.TextField = "Title";

    // Create the parent node binding.
    TreeNodeBinding ParentBinding = new TreeNodeBinding();
    ParentBinding.DataMember = "Chapter";
    ParentBinding.TextField = "Heading";

    // Create the leaf node binding.
    TreeNodeBinding LeafBinding = new TreeNodeBinding();
    LeafBinding.DataMember = "Section";
    LeafBinding.TextField = "Heading";

    // Add bindings to the DataBindings collection.
    NewTree.DataBindings.Add(RootBinding);
    NewTree.DataBindings.Add(ParentBinding); 
    NewTree.DataBindings.Add(LeafBinding);

    // Manually register the event handler for the SelectedNodeChanged event.
    NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);

    // Add the TreeView control to the Controls collection of the PlaceHolder control.
    ControlPlaceHolder.Controls.Add(NewTree);

  }

  void Node_Change(Object sender, EventArgs e)
  {

    // Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
    TreeView LocalTree = (TreeView)ControlPlaceHolder.FindControl("BookTreeView");

    // Display the selected node.
    Message.Text = "You selected: " + LocalTree.SelectedNode.Text;

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>States</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>TreeView Constructor Example</h3>

      <asp:PlaceHolder id="ControlPlaceHolder" runat="server">
      </asp:PlaceHolder>

      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>

      <br /><br />

      <asp:Label id="Message" runat="server"/>

    </form>
  </body>
</html>

see also https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.treeview.-ctor?view=netframework-4.7.2

Sign up to request clarification or add additional context in comments.

Comments

1

Create a simple object structure. A List of States, for example. State class itself containing the State properties and a list of cities object.

Then use it with a web component displaying a List with for each list item, an inner detail list. You can maybe find one or create it yourself since your hierarchy has only one level.

1 Comment

i need a tree view not list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.