After I wrote this article last time, I immediately noticed an error, that is, there may be node leakage. When a node is added, there is only one loop. When a node is added, the parent node may not be added, that is, the parent node cannot be found, which leads to missed addition.
Sorry, I'm not careful. However, I usually do it casually. If I don't want to be mature, I will do it. But if I find something wrong, I will definitely correct it. If you don't like me, leave the article aside.
Original program:
/// <Summary>
/// Reset the Treeview of the product category
/// Resetsortview () function
/// </Summary>
# Region resetsortview () function implementation
Private void resetsortview ()
{
Trvsort. nodes. Clear ();
Arrnode. Clear ();
Exnode nD = new exnode ();
//
// Add product category
//
Sort mysort = new sort ();
Mysort. ID = 0;
Mysort. Name = "commodity class ";
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 the database
// Sorry, I have defined all classes for opening the database and packaged them in dbclass.
// Replace SQL Server with less effort
// Dataset is also encapsulated here, which is mydb. dbdataset
// You are too lazy to determine the database. If you are not familiar with the database, learn it quickly.
String SQL = "select * From merchandisesort order by merchandisesortid ";
Dbclass mydb = new dbclass ();
Mydb. dbopen ();
Mydb. createadapter (SQL );
Mydb. filldataset ();
//
// Add data records to the tree one by one
// Error starts from here
// Configure //--------------------------------------------------------------------------------------
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 );
}
// Configure //--------------------------------------------------------------------------------------
Mydb. dbclose ();
Trvsort. expandall ();
}
This code is rewritten to delete the node record from dataset and perform a new loop after successfully adding one to Treeview. Otherwise, dataset searches for the record that can be added. The procedure is as follows:
Subprograms for adding nodes after correction:
/// <Summary>
/// Add a node to the Treeview and add the node to the array for convenient query
/// </Summary>
Private bool addnode (sort addsort)
{
Bool added = false;
Exnode parentnode = new exnode (); // The parent node to be attached
Exnode addnode = new exnode (); // current node
Addnode. Sort = addsort;
If (addnode. Sort. parentid = 0)
{
Trvsort. nodes [0]. nodes. Add (addnode );
// Set the flag to locate
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 locate
Added = true;
Break;
}
}
}
// If no value is found, false is returned.
If (! Added) return false;
If (addsort. isend)
{
If (addsort. Disable)
{
Addnode. imageindex = 4;
Addnode. selectedimageindex = 4;
Addnode. forecolor = systemcolors. graytext;
}
Else
{
Addnode. imageindex = 2;
Addnode. selectedimageindex = 2;
Addnode. forecolor = systemcolors. windowtext;
}
}
Else
{
If (addsort. Disable)
{
Addnode. imageindex = 3;
Addnode. selectedimageindex = 3;
Addnode. forecolor = systemcolors. graytext;
}
Else
{
Addnode. imageindex = 1;
Addnode. selectedimageindex = 1;
Addnode. forecolor = systemcolors. windowtext;
}
}
Return true;
}
Corrected Classification Tree display program:
/// <Summary>
/// Reset the Treeview of the product category
/// </Summary>
Public void resetsortview ()
{
Trvsort. nodes. Clear ();
Arrnode. Clear ();
Exnode nD = new exnode ();
//
// Add product category
//
Sort mysort = new sort ();
Mysort. ID = 0;
Mysort. Name = "commodity class ";
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 ();
//
// After the following code is corrected, the dual loop is used, until all the records in the dataset are added.
//
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 it is successfully added, the consumer deletes the corresponding records in the dataset and enters a new loop.
If (addnode (mysort ))
{
Mydb. dbdataset. Tables [0]. Rows. removeat (I-1 );
Mydb. dbdataset. Tables [0]. acceptchanges ();
Break;
}
}
}
Mydb. dbclose ();
Trvsort. collapseall ();
Trvsort. nodes [0]. Expand ();
}