简体   繁体   中英

how to use hierarchy id in c# windows form application

i create a simple table such as image below

在此处输入图像描述

and fill by item such as image below

在此处输入图像描述

load this table in tree view on c# form application by using microsoft.sqlserver.types.10.50.1600.1 nuget and work fin

在此处输入图像描述

my problem is i don't know how to convert tree view to hierarchyid in c#. i search on internet but all of description for hierarchyid in sql-server. i use tree view path but that return string for example: node car repairman return:Manager/NT/NT& Transportation Expert/Car repairman but i want return /1/1/2/ to store in my table and second problem that how to traversal all brunch node to update table when insert or delete. my code to load data to treeview is:

namespace TVHierachyID
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dataTable4;
        private BindingSource oBS = new BindingSource();
        private void btnGetData_Click(object sender, EventArgs e)
        {
            SqlConnection cs4 = new SqlConnection("Data Source=1.1.1.1;Network Library=DBMSSOCN;Initial Catalog=test;User ID=test;Password=test");
            SqlDataAdapter adapter4 = new SqlDataAdapter("select unit_name,lvl,lvl.ToString()as lvls,lvl.GetLevel()as lvln from test_tbl", cs4);
             dataTable4 = new DataTable();
            adapter4.Fill(dataTable4);            
            oBS.DataSource = dataTable4;
            dgData.DataSource = oBS;
            lblCount.Text = string.Format("{0} records loaded", oBS.Count);
        }
        private void doTV_Click(object sender, EventArgs e)
        {
            string sKeyField = "lvl", sTextField = "lvls";
            LoadTreeSQLHierarchy(this.tvData, dataTable4,sKeyField,sTextField);
        }
        /// <summary>
        /// Uses Linq to filter the table
        /// additional info from: http://nesteruk.org/blog/post/Working-with-SQL-Server-hierarchical-data-and-Silverlight.aspx#Reference6
        /// </summary>
        /// <param name="oTV">Treeview to load</param>
        /// <param name="oTable">datatable with the nodekey</param>
        private void LoadTreeSQLHierarchy(TreeView oTV, DataTable oTable, string sKeyField, string sTextField)
        {
            oTV.Nodes.Clear();
            TreeNode oNode;
            SqlHierarchyId iID = new SqlHierarchyId();
            EnumerableRowCollection<DataRow> query = from TNodes in oTable.AsEnumerable()                                                                                            where TNodes.Field<SqlHierarchyId>(sKeyField).GetAncestor(1).Equals(iID)
                                                     select TNodes;

            DataView oDV = query.AsDataView();
            if (oDV.Count == 1)
            {               
                oNode = new TreeNode(oDV[0][0].ToString());
                oNode.Tag = oDV[0].Row;
                LoadNodeSQLHierarchy(oNode, oTable);
                oTV.Nodes.Add(oNode);
            }
        }
        /// <summary>
        /// Load up the children 
        /// </summary>
        /// <param name="oParent">parent node</param>
        /// <param name="oTable">datatable with the nodekey</param>
        private void LoadNodeSQLHierarchy(TreeNode oParent, DataTable oTable)
        {
            oParent.Nodes.Clear();
            SqlHierarchyId iID = new SqlHierarchyId();
            DataRow oRow = (DataRow)oParent.Tag;
            iID = (SqlHierarchyId)oRow["lvl"];
            EnumerableRowCollection<DataRow> query = from order in oTable.AsEnumerable()
                                                     where order.Field<SqlHierarchyId>("lvl").GetAncestor(1).Equals(iID)
                                                     select order;
            DataView oDV = query.AsDataView();
            foreach (DataRowView oDR in oDV)
            {
                TreeNode oNode = new TreeNode(oDR["unit_name"].ToString());
                oNode.Tag = oDR.Row;
                LoadNodeSQLHierarchy(oNode, oTable);
                oParent.Nodes.Add(oNode);
            }
        }       
    }
}

solved by this Maybe others will use it:X

private void buttonsave_Click(object sender, EventArgs e)
    {
        TreeNode oMainNode = tvData.Nodes[0];
        PrintNodesRecursive(oMainNode);
        fsave = true;
    }

 public void PrintNodesRecursive(TreeNode oParentNode)
    {
        // Start recursion on all subnodes.
        foreach (TreeNode oSubNode in oParentNode.Nodes)
        {
            TreeNode node = oSubNode;
            while (node != null)
            {
                path2.Push(node.Index.ToString());
                node = node.Parent;
            }
            string nodePath = string.Join("/", path2.ToArray()) + "/";
            nodePath = nodePath.Remove(0, 1);
            MessageBox.Show(nodePath, oSubNode.Text);
            path2.Clear();
            PrintNodesRecursive(oSubNode);
        }
    }

This might be a slightly cleaner way:

public IEnumerable<TreeNode> GetTreeNodeParentPath(TreeNode oParentNode)
{
    if (oParentNode != null)
    {
        yield return oParentNode;
        foreach (var node in GetTreeNodeParentPath(oParentNode.Parent))
            yield return node;
    }
}

public void PrintNodesRecursive(TreeNode oParentNode)
{
    foreach (TreeNode oSubNode in oParentNode.Nodes)
    {
        var message = String.Concat(GetTreeNodeParentPath(oSubNode).Select(x => $"{x.Index}/"));
        MessageBox.Show((message), oSubNode.Text);
        PrintNodesRecursive(oSubNode);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM