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