![](/img/trans.png)
[英]How to get Id from ComboBox in C# windows Form Application
[英]how to use hierarchy id in c# windows form application
我创建了一个简单的表格,如下图所示
并按项目填写,如下图
使用 microsoft.sqlserver.types.10.50.1600.1 nuget 在 c# 表单应用程序的树视图中加载此表并工作
我的问题是我不知道如何在 c# 中将树视图转换为层次结构。 我在互联网上搜索,但 sql-server 中的hierarchyid 的所有描述。 我使用树视图路径,但返回字符串,例如:节点汽车修理工返回:经理/NT/NT& 运输专家/汽车修理工,但我想返回 /1/1/2/ 存储在我的表中,第二个问题是如何遍历插入或删除时更新表的所有早午餐节点。 我将数据加载到 treeview 的代码是:
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);
}
}
}
}
通过这个解决了也许其他人会使用它: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);
}
}
这可能是一种更清洁的方式:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.