Implementation of some functions of Treelist
1. The two most basic properties of data binding: KeyFieldName and ParentFieldName. (These two properties can basically be graded as soon as they are set)
It can be implemented through code writing, or directly in properties.
This kind of database design is relatively common. Generally, the data can be designed in this way if it satisfies a tree relationship. When binding data, you only need to specify DataSource as the corresponding DataTable, specify KeyFieldName as the table primary key field, and ParentFieldName as the foreign key field name that the table points to the primary key.
private void BindData()
{
= dtOffice;
= "OfficeID";
// = "OfficeName";
["OfficeName"].Caption = "Office Name";
= "ParentOfficeID";
}
2 Implementation of basic functions
①. When selecting a certain node, all children of the node are selected. When canceling a certain node, all children of the node are cancelled.
Which node causes behavior? Is the node selected or unchecked? This determines two parameters of the method: TreeListNode and CheckState. Just traverse the node and its descendants and set its selected state to the state of the node.
/// <summary>
/// When selecting a certain node, all children of the node are selected. When canceling a certain node, all children of the node are cancelled.
/// </summary>
/// <param name="node"></param>
/// <param name="state"></param>
private void SetCheckedChildNodes(TreeListNode node, CheckState check)
{
for (int i = 0; i < ; i++)
{
[i].CheckState = check;
SetCheckedChildNodes([i], check);
}
}
②. When all children of a node are selected, the node is selected; when all children of a node are not selected, the node is not selected.
/// <summary>
/// When all children of a node are selected, the node is selected. When all children of a node are not selected, the node is not selected.
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedParentNodes(TreeListNode node, CheckState check)
{
if ( != null)
{
CheckState parentCheckState = ;
CheckState nodeCheckState;
for (int i = 0; i < ; i++)
{
nodeCheckState = (CheckState)[i].CheckState;
If (!(nodeCheckState))//As long as any one is different from its selected state, the parent node status is not selected.
{
parentCheckState = ;
break;
}
parentCheckState = check;// Otherwise (the selected status of the brother nodes of the node is the same), the selected status of the parent node is the selected status of the node
}
= parentCheckState;
SetCheckedParentNodes(, check);//Travel over the upper node
}
}
The previous two steps have been written, don't forget that the above two methods are triggered in TreeList_AfterCheckNode:
private void tlOffice_AfterCheckNode(object sender, e)
{
SetCheckedChildNodes(, );
SetCheckedParentNodes(, );
}
3. Get the selected checkbox data list
private List<int> lstCheckedOfficeID = new List<int>();//Select the home ID collection
/// <summary>
/// Get the data primary key ID set of selected state
/// </summary>
/// <param name="parentNode">parent node</param>
private void GetCheckedOfficeID(TreeListNode parentNode)
{
if ( == 0)
{
return;//Recursive termination
}
foreach (TreeListNode node in )
{
if ( == )
{
DataRowView drv = (node) as DataRowView;
//The key code is just because I don’t know how to get data, and I have been struggling for a long time (I know that it can be converted to DataRowView)
if (drv != null)
{
int OfficeID = (int)drv["OfficeID"];
(OfficeID);
}
}
GetCheckedOfficeID(node);
}
}
The following test gets the primary key list:
private void btnCheck_Click(object sender, EventArgs e)
{
();
if ( > 0)
{
foreach (TreeListNode root in )
{
GetCheckedOfficeID(root);
}
}
string idStr = ;
foreach (int id in lstCheckedOfficeID)
{
idStr += id + " ";
}
(idStr);
}