Save the treeview data to tables and xml, and set the treenode tag of the treeview according to the rule.

Source: Internet
Author: User

# Region saves treeview data to the table
/// <Summary>
/// Save the treeview to the table. Because the table is an object, it is passed as a reference and no return value is required.
/// </Summary>
/// <Param name = "treeView1"> input tree </param>
/// <Param name = "strParentNumberFieldtext"> parent node tag </param>
/// <Param name = "strNumberFieldtext"> current node tag </param>
/// <Param name = "strNameFieldtext"> current node text </param>
/// <Returns> </returns>
Public DataTable SaveToDataTable (TreeView treeView1, string strParentNumberFieldtext, string strNumberFieldtext, string strNameFieldtext)
{
DataTable dt = new DataTable ("treeTable ");

DataColumn parentNumtag = new DataColumn (strParentNumberFieldtext, typeof (System. String ));
ParentNumtag. AllowDBNull = true;
ParentNumtag. MaxLength = 40;
DataColumn numTag = new DataColumn (strNumberFieldtext, typeof (System. String ));
NumTag. AllowDBNull = false;
NumTag. MaxLength = 40;
DataColumn nametext = new DataColumn (strNameFieldtext, typeof (System. String ));
Nametext. MaxLength = 40;
Dt. Columns. Add (parentNumtag );
Dt. Columns. Add (numTag );
Dt. Columns. Add (nametext );
ForTreeViewToData (treeView1, dt );

Return dt;

}

/// <Summary>
/// Save each node tag in the treeview to dt
/// </Summary>
/// <Param name = "treeView1"> the treeview to save </param>
/// <Param name = "dt"> saved dt </param>
/// <Returns> </returns>
Private void ForTreeViewToData (TreeView treeView1, DataTable dt)
{
Foreach (TreeNode treenode in treeView1.Nodes)
{
SavaTreeNodeToTableColumn (treenode, dt );
}

}

/// <Summary>
/// Save each node tag to the table to determine whether the current node is a parent node. If the first parent node is 01 and the second is 02, the child nodes are 0101,0102 respectively.
/// </Summary>
/// <Param name = "treenode"> tree node </param>
/// <Param name = "dt"> saved table </param>
Private void SavaTreeNodeToTableColumn (TreeNode treenode, DataTable dt)
{
DataRow dr = dt. NewRow ();
// Whether a parent node exists
If (treenode. Parent = null)
{
Dr [0] = "";
}
Else
{
// Save the tag of the parent node to the parent flag
Dr [0] = treenode. Parent. Tag. ToString ();

}
Dr [1] = treenode. Tag. ToString ();
Dr [2] = treenode. Text. ToString ();
Dt. Rows. Add (dr );
// Recursively save each subnode to the table
Foreach (TreeNode tempnode in treenode. Nodes)
{
SavaTreeNodeToTableColumn (tempnode, dt );
}

}
# Endregion

# Region set tags for treeview

/// <Summary>
/// Set the value for all treeview treeNode
/// </Summary>
/// <Param name = "treeView"> input treeview object </param>
/// <Returns> whether the setting is successful </returns>
Public bool SetTreeViewTag (TreeView treeView)
{
Try
{
Foreach (TreeNode treeNode in treeView. Nodes)
{
// Set a tag for each tag
SetTreeNodeTag (treeNode );
}

Return true;
}
Catch (Exception ex)
{
Return false;

// Throw;
}

}
/// <Summary>
/// Set the tag value for the current node. Recursion of all subnodes under the node
/// </Summary>
/// <Param name = "treeNode"> input node </param>
Private void SetTreeNodeTag (TreeNode treeNode)
{

SetValueToTreeNode (treeNode );
Foreach (TreeNode tempTreenode in treeNode. Nodes)
{
SetTreeNodeTag (tempTreenode );
}
}

/// <Summary>
/// Set the tag for a node in the format of 01,0101, 02,0201
/// </Summary>
/// <Param name = "treeNode"> assign a value to a node </param>
Private void SetValueToTreeNode (TreeNode treeNode)
{
String Ttag = "";
// Whether s is the root node
If (treeNode. Parent = null)
{
// Whether it is the first node of the subnode
If (treeNode. PrevNode = null)
{
Ttag = "01 ";
}
Else
{
Ttag = (Convert. toInt64 (treeNode. prevNode. tag. toString () + 1 ). toString (). padLeft (treeNode. prevNode. tag. toString (). length, '0 ');
}
}
Else
{
If (treeNode. PrevNode = null)
{
Ttag = treeNode. Parent. Tag. ToString () + "01 ";
}
Else
{
Ttag = (Convert. toInt64 (treeNode. prevNode. tag. toString () + 1 ). toString (). padLeft (treeNode. prevNode. tag. toString (). length, '0 ');
}

}
TreeNode. Tag = Ttag;
}

 

# Endregion

# Region Save the treeview to the XML file
/// <Summary>
/// Save the treeview to the Xml file
/// </Summary>
/// <Param name = "treeView1"> saved treeview </param>
/// <Param name = "xmlPath"> Save the XML Path </param>
/// <Param name = "strParentNumberFieldtext"> tag of the parent node </param>
/// <Param name = "strNumberFieldtext"> tag of the current node </param>
/// <Param name = "strNameFieldtext"> text of the current node </param>
/// <Returns> </returns>
Public bool SaveTreeViewToXml (TreeView treeView1, string xmlPath, string strParentNumberFieldtext, string strNumberFieldtext, string strNameFieldtext)
{
Try
{
DataTable dt = SaveToDataTable (treeView1, strParentNumberFieldtext, strNumberFieldtext, strNameFieldtext );
If (dt! = Null)
{
Dt. WriteXml (xmlPath );
}
Return true;
}
Catch
{

Return false;
}


}
# Endregion

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.