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);
}
}
}
}