#region 将treeview的数据保存到表中
/// <summary>
/// 保存treeview到表中,因table是对象故是引用传递,不用返回值。
/// </summary>
/// <param name="treeView1">传入的树</param>
/// <param name="strParentNumberFieldtext">父节点tag</param>
/// <param name="strNumberFieldtext">当前节点tag</param>
/// <param name="strNameFieldtext">当前节点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>
/// 将treeview中的每个节点tag保存到dt
/// </summary>
/// <param name="treeView1">要保存的treeview</param>
/// <param name="dt">保存的dt</param>
/// <returns></returns>
private void ForTreeViewToData(TreeView treeView1, DataTable dt)
{
foreach(TreeNode treenode in treeView1.Nodes)
{
SavaTreeNodeToTableColumn(treenode, dt);
}
}
/// <summary>
/// 将每个节点tag保存到表中判断当前节点是不是父节点。如果是第一个父节点是01第二个是02.子节点分别是0101,0102
/// </summary>
/// <param name="treenode">树节点</param>
/// <param name="dt">保存的表</param>
private void SavaTreeNodeToTableColumn(TreeNode treenode, DataTable dt)
{
DataRow dr = dt.NewRow();
//是否有父节点
if (treenode.Parent == null)
{
dr[0] = "";
}
else
{
//将父节点的tag保存草父标记中
dr[0] = treenode.Parent.Tag.ToString();
}
dr[1] = treenode.Tag.ToString();
dr[2] = treenode.Text.ToString();
dt.Rows.Add(dr);
//递归将每个子节点都保存到表中
foreach (TreeNode tempnode in treenode.Nodes)
{
SavaTreeNodeToTableColumn(tempnode, dt);
}
}
#endregion
#region 为treeview设置tag
/// <summary>
/// 为所有的treeview的treeNode设置值
/// </summary>
/// <param name="treeView">传入的treeview对象</param>
/// <returns>是否设置成功</returns>
public bool SetTreeViewTag(TreeView treeView)
{
try
{
foreach (TreeNode treeNode in treeView.Nodes)
{
//为每个tag设置tag
SetTreeNodeTag(treeNode);
}
return true;
}
catch (Exception ex)
{
return false;
//throw;
}
}
/// <summary>
/// 现为当前节点设置tag值。再递归该节点下的所有子节点
/// </summary>
/// <param name="treeNode">传入节点</param>
private void SetTreeNodeTag(TreeNode treeNode)
{
SetValueToTreeNode(treeNode);
foreach (TreeNode tempTreenode in treeNode.Nodes)
{
SetTreeNodeTag(tempTreenode);
}
}
/// <summary>
/// 具体为某个节点设置tag 是以01,0101,02,0201格式的
/// </summary>
/// <param name="treeNode">具体为节点赋值</param>
private void SetValueToTreeNode(TreeNode treeNode)
{
string Ttag="";
//s是否为根节点
if (treeNode.Parent == null)
{
//是否为子节点的第一个节点
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 保存treeview到XML文件中
/// <summary>
/// 保存treeview到Xml文件中
/// </summary>
/// <param name="treeView1">保存的treeview</param>
/// <param name="xmlPath">保存XML的路径</param>
/// <param name="strParentNumberFieldtext">父节点的tag</param>
/// <param name="strNumberFieldtext">当前节点的tag</param>
/// <param name="strNameFieldtext">当前节点的text</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