Display hierarchical data in the Treeview Control

Source: Internet
Author: User
1. Create the following database connection string in the web. config file <connectionstrings>
<Add name = "northwindconnectionstring" connectionstring = "Data Source = localhost; initial catalog = northwind; Integrated Security = true"
Providername = "system. Data. sqlclient"/>
</Connectionstrings>

2. Add the following code to the Treeview page: protected void treeviewinclutreenodepopulate (Object sender, treenodeeventargs E)
{
If (E. node. childnodes. Count = 0)
{
Switch (E. node. Depth)
{
Case 0:
Populatecategories (E. node );
Break;
Case 1:
Populateproducts (E. node );
Break;
}
}
}

This code is called when you click a node to open the node. Because different data needs to be displayed at different levels of the tree, you must determine the depth of the node clicked by the user, and then fill in the nodes at this level as appropriate. During this drill, if you click the root node (depth is 0 ),PopulateCategoriesMethod. If you click the category name (depth: 1 ),PopulateProductsMethod. These methods are demonstrated in the next section. The object provides programming access to the current node. To fill nodes, add elements to the nodes. In this code example, the node is passed to the method, and the method adds a subnode.

Void populatecategories (treenode node)
{
Sqlcommand sqlquery = new sqlcommand (
"Select categoryname, categoryid from categories ");
Dataset resultset;
Resultset = runquery (sqlquery );
If (resultset. Tables. Count> 0)
{
Foreach (datarow row in resultset. Tables [0]. Rows)
{
Treenode newnode = new
Treenode (row ["categoryname"]. tostring (),
Row ["categoryid"]. tostring ());
Newnode. populateondemand = true;
Newnode. selectaction = treenodeselectaction. Expand;
Node. childnodes. Add (newnode );
}
}
}

This code is created Object, which encapsulates the query text. The Code passes the object to a method to be written later. The method executes the database query and returns Object. This code is then traversedDatasetObject, create a new node for each record, and set the text and value of the node with database information. Then, the code Set propertyTrueSo that when a node is clickedTreenodepopulateEvent. Property is set to enable nodes to expand by default.

The second-level node displays products of each category. For this reason, parameter-based query is required for filling product nodes so that you can retrieve products of the current category and populate the child nodes in an appropriate way.

Void populateproducts (treenode node)
{
Sqlcommand sqlquery = new sqlcommand ();
Sqlquery. commandtext = "select productname from products" +
"Where categoryid = @ categoryid ";
Sqlquery. Parameters. Add ("@ categoryid", sqldbtype. INT). value =
Node. value;
Dataset resultset = runquery (sqlquery );
If (resultset. Tables. Count> 0)
{
Foreach (datarow row in resultset. Tables [0]. Rows)
{
Treenode newnode = new
Treenode (row ["productname"]. tostring ());
Newnode. populateondemand = false;
Newnode. selectaction = treenodeselectaction. None;
Node. childnodes. Add (newnode );
}
}
}

This code is similar to the code used to fill in class nodes. One of the differences is thatSqlcommandThe object configuration has a parameter. during runtime, this parameter is set based on the value of the node clicked by the user (that is, the selected category. Another difference is thatPopulateondemandSet propertyFalse. This causes the Product node to be displayed without the expand button. This is required because there are no nodes under the product.

Private dataset runquery (sqlcommand sqlquery)
{
String connectionstring =
Configurationmanager. connectionstrings
["Northwindconnectionstring"]. connectionstring;
Sqlconnection dbconnection =
New sqlconnection (connectionstring );
Sqldataadapter dbadapter = new sqldataadapter ();
Dbadapter. selectcommand = sqlquery;
Sqlquery. Connection = dbconnection;
Dataset resultsdataset = new dataset ();
Try
{
Dbadapter. Fill (resultsdataset );
}
Catch
{
Labelstatus. Text = "unable to connect to SQL Server .";
}
Return resultsdataset;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.