Opt In (Do Not Edit Here)

Showing posts with label TreeView. Show all posts
Showing posts with label TreeView. Show all posts

Jan 12, 2013

Populating TreeView Control from XML file


Description:
The blog post demonstrates how to populate TreeView Control using XML file in C#.NET.

The TreeView control has a Nodes collection with root TreeNode objects. Each TreeNode in turn has its own Nodes collection that holds more than one child TreeNode.

<TreeNode object>.Nodes.Add (TreeNode node) method adds a new TreeNode element with a specified text to the end of the current TreeNode collection.

Procedure:
Step 1: Variables Declaration
DataSet set = new DataSet();  //To store Tables from XML Document

TreeNode parent;              //store Main Table Names
TreeNode child;               //store Contents of Main Table
Listing 1

Step 2: XML file contents tables as shown in below format.
<Customer>
      <Customer CustID="202" Name="Pratik Ghanwat" Contact="8657413501"/>
      <Customer CustID="203" Name="Sohan Dal"      Contact="9746292005"/>
      <Customer CustID="204" Name="Sujit Dalvi"    Contact="8855572178"/>
      <Customer CustID="205" Name="Sushant Shinde" Contact="9835621704"/>
            <Customer CustID="206" Name="Amey Kulkarni"  Contact="9845671203"/>
      </Customer>
Listing 2

Step 3: Now retrieve the values from XML File and add it to the Nodes Collection of TreeView Control.
//Read Contents of XML Document
set.ReadXml(@"..\..\XMLData.xml");

for (int i = 0; i < set.Tables.Count; i++) 
{    
      //Get the Name of Main Table
      parent = treeView1.Nodes.Add(set.Tables[i].TableName);

      //Get Contents of Main Table
      for (int j = 1; j < set.Tables[i].Rows.Count; j++) 
                    child = parent.Nodes.Add(set.Tables[i].Rows[j][2].ToString());
      }
Listing 3

Intended Result:


Figure 1: Output


Summary:

In this writing, we learned how to populate content from XML file to TreeView Control in C#.

                                                                                                              








Aug 26, 2011

Create TreeNode at Runtime


This tip shows how to create a TreeNode to TreeView Control at runtime in C#.

Following code snippet creates a TreeNode and its sub nodes to TreeView Control programmatically. Firstly, it creates a 2 TreeNode Object named ParentNode & ChildNode and then adds 4 nodes and accordingly sub nodes to the TreeNode Object using for loop. ExpandAll () Method expands all the nodes present in the specified TreeView Control on Execution.

public void CreateTree()

{

TreeNode ParentNode = treeView1.Nodes.Add("Parent Node");

TreeNode ChildeNode;

for (int i = 1; i <= 4; i++)

{

ChildeNode = ParentNode.Nodes.Add("Child Node " + i.ToString());

for (int j = 1; j <= i; j++)

{

ChildeNode.Nodes.Add("Sub Node " + j.ToString());

}

}

ParentNode.ExpandAll();

}

Listing 1


Figure 1: Intended Result

TreeView Control Populating with SqlDataSource at runtime.



In Brief:

The article demonstrates how to populate TreeView Control using SqlDataSource at runtime in C#.NET. We can achieve intended result by simply executing SqlCommand Object either by ExecuteReader or ExecuteNonQuery method.

The TreeView control has a Nodes collection with root TreeNode objects. Each TreeNode in turn has its own Nodes collection that holds more than one child TreeNode.

.Nodes.Add (TreeNode node) method adds a new tree node with the specified text to the end of the current tree node collection

Procedure:

Step 1: Declaration of Connection, Command, and DataReader object

SqlConnection Conn = new SqlConnection("Data Source=.\\SQLExpress;

Initial Catalog=NorthWind; Integrated Security=True");

SqlDataReader rdr;

SqlCommand cmd;

Listing 1

Step 2: Now, whatever the code required to written for populating the SqlDataSource to the TreeView Control you can place it either into the click, load etc. event handlers.

Step 3: We need to create two TreeNode Objects as follows…

TreeNode parent = treeView1.Nodes.Add("Suppliers");

TreeNode child;

Listing 2

Step 4: Now pass the SqlQuery as an argument of SqlCommand Object and stored the values into SqlDataReader Object.

cmd = new SqlCommand("SqlQuery", Conn);

Conn.Open();

rdr = cmd.ExecuteReader();

Listing 3

Step 5: Now retrieve the values from SqlDataReader and adds it to the Child Nodes of Nodes Collection of TreeView Control.

while (rdr.Read())

{

child = parent.Nodes.Add("Supplier ID: " + rdr.GetValue(0).ToString());

child.Nodes.Add("Name: " + rdr.GetValue(1).ToString());

.

.

.

}

Listing 4

Intended Result:

Figure 1



All Rights Reserved. 2014 Copyright SIMPLITONA

Powered By Blogger | Published By Gooyaabi Templates Designed By : BloggerMotion

Top