如何在 C# 中的树视图中显示选定节点的显示值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8822851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show a display value for selected node in treeview in C#?
提问by
I am trying to create a treeview which pulls info from a sql database. I want the text to be the name field but when you double click the name i want it to display the id field. I have look and looked but cant find any info on this?
我正在尝试创建一个从 sql 数据库中提取信息的树视图。我希望文本是名称字段,但是当您双击名称时,我希望它显示 id 字段。我看了又看,但找不到任何有关此的信息?
Code tried (Added from OP's comment):
代码尝试(从 OP 的评论中添加):
foreach (DataRow dr in Db.Table("Employee").Rows)
{
treeView1.Nodes.Add(
new TreeNode(dr["Name"].ToString(),
new TreeNode[] {new TreeNode(dr["EEID"].ToString())}));
}
var node = treeView1.SelectedNode.Nodes[0].Text;
MessageBox.Show(string.Format("You selected: {0}", node));
采纳答案by CMPerez
foreach (DataRow dr in Db.Table("Employee").Rows)
{
TreeNode tn = new TreeNode();
tn.Tag = dr["eeid"];
tn.Text = dr["Name"].ToString();
treeView1.Nodes.Add(tn);
}
private void treeView1_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(treeView1.SelectedNode.Tag.ToString());
}
回答by user1120193
you can use Mouse Click event. when you click on a particular node (assume it's not WPF cause then it's Items) you can get its Text from SelectedNode property.
您可以使用鼠标单击事件。当您单击特定节点时(假设它不是 WPF 原因,则是项目),您可以从 SelectedNode 属性中获取其文本。
private void btnGetNodeValue_Click(object sender, EventArgs e)
{
string nodeVal= treeView1.SelectedNode.Text;
}
then you can pass this string value to database to retrieve your value,mix up with Select statement and WHERE clause so you can easily get it.
然后您可以将此字符串值传递给数据库以检索您的值,与 Select 语句和 WHERE 子句混合使用,以便您轻松获取它。
回答by CMPerez
When you create new nodes for a TreeView you can specify a text value and a key value, like so:
为 TreeView 创建新节点时,您可以指定文本值和键值,如下所示:
TreeView tv = new TreeView();
tv.Nodes.Add(key, text); //where key is your database id value, and text the display
Then you'd simply return the key of the clicked node. Is this what you want?
然后您只需返回单击节点的键。这是你想要的吗?
EDIT:This is what happens when you speak from memory... this is wrong. 'key' is not a hidden key value, like an ID, 'key' is the name of the tree node. Please hold while I give you a proper solution.
编辑:这就是当你凭记忆说话时会发生的事情......这是错误的。'key' 不是一个隐藏的键值,就像一个 ID,'key' 是树节点的名称。请稍等,我给你一个合适的解决方案。
** EDIT2 (SOLVED) ** : You can also use the Name property. Like this:
** EDIT2(已解决)**:您还可以使用 Name 属性。像这样:
tView.Nodes.Add("Id_0001", "Mr. Dexter");
then you could retrieve the values of that node with something like this:
然后您可以使用以下内容检索该节点的值:
private void tvView_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode node = e.Node;
MessageBox.Show(node.Name + "\n" + node.Text);
}
which would yield the results: "Id_0001" and "Mr. Dexter".
这将产生结果:“Id_0001”和“德克斯特先生”。

