Provincial/Municipal Data is recursively loaded to Treeview

Source: Internet
Author: User
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. data. sqlclient;

Namespace provincial and municipal data is recursively loaded to Treeview {public partial class form1: FORM {public form1 () {initializecomponent ();} private void button#click (Object sender, eventargs E) {// 1. load all provinces and municipalities to the root node of the Treeview. // List <area> listprovince = getsubitembyparentid (0); // foreach (area item in listprovince) // {// treeview1.nodes. add (item. areaname); // recursively load provinces and cities to the Treeview loaddatatotree (treeview1.nodes, 0);} // recursively load to the Treeview private void loaddatatotree (treenodecollection, int PID) {// 1. obtain the sub-city list <area> listcity = getsubitembyparentid (PID) of the City Based on the specified PID; // 2. traverse to bind the obtained data to the treenodecollection Node Set In foreach (VAR item in listcity) {// return the added node treenode node = treenodecollection. add (item. areaname); loaddatatotree (node. nodes, item. areaid) ;}}// encapsulate a method to query all subitem private list <area> getsubitembyparentid (int pid) based on the parent ID) {list <area> List = new list <area> (); string SQL = "select * From tblarea where [email protected]"; using (sqldatareader reader = sqlhelper. executereader (SQL, new sqlparameter (" @ PID ", pid) {If (reader. hasrows) {While (reader. read () {area model = new area (); Model. areaid = reader. getint32 (0); Model. areaname = reader. getstring (1); model. areapid = reader. getint32 (2); list. add (model); // do not recursion here directly. You must first obtain the data to a set, then recursion // otherwise many connections will be opened. }}} Return list;} public class area {public int areaid {Get; set;} Public String areaname {Get; set;} public int areapid {Get; set ;}}}

 

Provincial/Municipal Data is recursively loaded to Treeview

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.