The amendment procedure of realizing infinite class classification by C # and database
Source: Internet
Author: User
Program | data | Database The last time you wrote this article, you immediately noticed that there was a mistake that there might be a node leak. Because when the node is added, there is only one loop, when the node is added, the parent node may not have been added, that is, unable to find the father, which caused the leak.
I'm sorry, I'm not careful. But I am usually very casual, one thing did not want to mature to do, but found that the wrong, will certainly be revised. If you don't like me, just throw the article aside.
Original program:
<summary>
Re-set the TreeView of the commodity category
Resetsortview () function
</summary>
#region Resetsortview () function implementation
private void Resetsortview ()
{
TrvSort.Nodes.Clear ();
Arrnode.clear ();
Exnode nd = new Exnode ();
//
Add the general category of goods
//
Sort Mysort = new sort ();
mysort.id = 0;
Mysort.name = "Commodity general category";
Mysort.parentid =-1;
Mysort.isend =false;
Mysort.disable = false;
nd. Sort = Mysort;
nd. ImageIndex = 0;
nd. selectedImageIndex = 0;
TRVSORT.NODES.ADD (ND);
Arrnode.add (ND);
Open Database
Sorry, I have to open the database has been defined class, fully packaged in the Dbclass
It's less effort to switch to SQL Server.
The dataset is also encapsulated here, for Mydb.dbdataset
Lazy to set the database, if not familiar with the database, quickly learn
String sql = "SELECT * from Merchandisesort order by Merchandisesortid";
Dbclass MyDB = new Dbclass ();
Mydb.dbopen ();
Mydb.createadapter (SQL);
Mydb.filldataset ();
//
Add the data record to the tree
The error starts here
// --------------------------------------------------------------------------------------
for (int i = 1; I <= mydb.dbdataset.tables[0]. Rows.Count; i++)
{
mysort.id = (int) mydb.dbdataset.tables[0]. rows[i-1]["Merchandisesortid"];
Mysort.name = Mydb.dbdataset.tables[0]. rows[i-1]["Name"]. ToString ();
Mysort.parentid = (int) mydb.dbdataset.tables[0]. rows[i-1]["ParentID"];
Mysort.isend = (bool) mydb.dbdataset.tables[0]. rows[i-1]["Isend"];
Mysort.disable = (bool) mydb.dbdataset.tables[0]. rows[i-1]["Disable"];
AddNode (Mysort);
}
// --------------------------------------------------------------------------------------
Mydb.dbclose ();
Trvsort.expandall ();
}
The code overrides, that is, after a successful addition to the TreeView, delete the node record from the dataset and make a new loop, otherwise the dataset looks down to the records that can be added. The procedure is as follows:
Modified subroutine for adding nodes:
<summary>
Add node to the TreeView and add node to the array for easy query
</summary>
private bool AddNode (Sort addsort)
{
BOOL Added = false;
Exnode parentnode = new Exnode (); The parent node to hook up
Exnode AddNode = new Exnode (); This node
Addnode.sort = AddSort;
if (AddNode.Sort.ParentID = 0)
{
Trvsort.nodes[0]. Nodes.Add (AddNode);
Set the flag to find
Added = true;
Arrnode.add (AddNode);
Addnode.idpath = "root\\0";
}
Else
{
foreach (Exnode pnode in Arrnode)
{
if (PNode.Sort.ID = = Addsort.parentid)
{
ParentNode = Pnode;
PARENTNODE.NODES.ADD (AddNode);
Arrnode.add (AddNode);
Addnode.idpath = Parentnode.idpath + "\" + addNode.Sort.ParentID.ToString ();
Set the flag to find
Added = true;
Break
}
}
}
Returns False if it is not found
if (! Added) return false;
<summary>
Re-set the TreeView of the commodity category
</summary>
public void Resetsortview ()
{
TrvSort.Nodes.Clear ();
Arrnode.clear ();
Exnode nd = new Exnode ();
//
Add the general category of goods
//
Sort Mysort = new sort ();
mysort.id = 0;
Mysort.name = "Commodity general category";
Mysort.parentid =-1;
Mysort.isend =false;
Mysort.disable = false;
nd. Sort = Mysort;
nd. ImageIndex = 0;
nd. selectedImageIndex = 0;
TRVSORT.NODES.ADD (ND);
Arrnode.add (ND);
nd. Idpath = "root";
String sql = "SELECT * from Merchandisesort ORDER by Merchandisesortid ASC";
Dbclass MyDB = new Dbclass ();
Mydb.dbopen ();
Mydb.createadapter (SQL);
Mydb.filldataset ();
//
The following revised code, with a double loop, true to all the records in the dataset to add until
//
while (mydb.dbdataset.tables[0). Rows.Count > 0)
{
for (int i = 1; I <= mydb.dbdataset.tables[0]. Rows.Count; i++)
{
mysort.id = (int) mydb.dbdataset.tables[0]. rows[i-1]["Merchandisesortid"];
Mysort.name = Mydb.dbdataset.tables[0]. rows[i-1]["Sortname"]. ToString ();
Mysort.parentid = (int) mydb.dbdataset.tables[0]. rows[i-1]["ParentID"];
Mysort.isend = (bool) mydb.dbdataset.tables[0]. rows[i-1]["Isend"];
Mysort.disable = (bool) mydb.dbdataset.tables[0]. rows[i-1]["Disable"];
If you add success, delete the corresponding record in the dataset and enter the new loop
if (AddNode (Mysort))
{
Mydb.dbdataset.tables[0]. Rows.removeat (i-1);
Mydb.dbdataset.tables[0]. AcceptChanges ();
Break
}
}
}
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