SIGN UP MEMBER LOGIN:    
ARTICLE

TreeView Control Populating with SqlDataSource

Posted by Mayur Dighe Articles | Windows Controls C# August 21, 2011
The Article describes populating TreeView Control with SqlDataSource at runtime.
Reader Level:
Download Files:
 

Description:

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.

<TreeNode object>.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:

Treeview.gif

Figure 1

Summary:

In this article, we discussed how we can populate a DataTable from the SqlDataSource into TreeView Control in C#.

Title Scrolling: http://www.vbdotnetheaven.com/UploadFile/satyapriyanayak/8684/

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor