C#中如何实现TreeView控件选中根结点相应的子节点也选中,同时将信息相应的显示到DataGridView控件中。
就是如下图,当选中一班时对应的三个节点也选上,每个节点若被选中,则将选中节点的信息显示到DataGridView中。高分跪求源代码。。。。。...
就是如下图,当选中一班时对应的三个节点也选上,每个节点若被选中,则将选中节点的信息显示到DataGridView中。
高分跪求源代码。。。。。 展开
高分跪求源代码。。。。。 展开
3个回答
展开全部
#region 将树的checkbox选中
private void setNodeTrue(Node selNode)
{
Node node = selNode.Parent;
if (node != null)
{
node.CheckState = CheckState.Checked;
setNodeTrue(node);
}
}
#endregion
#region 将树的checkbox选中
private void setNodeStateTrue(Node selNode)
{
if (selNode.CheckBoxVisible)
{
selNode.CheckState = CheckState.Checked;
foreach (Node node in selNode.Nodes)
{
setNodeStateTrue(node);
}
}
}
#endregion
#region 将树的checkbox取消选中
private void setNodeStateFalse(Node selNode)
{
if (selNode.CheckBoxVisible)
{
selNode.CheckState = CheckState.Unchecked;
foreach (Node node in selNode.Nodes)
{
setNodeStateFalse(node);
}
}
}
#endregion
#region 将树的checkbox取消选中
private void setNodeFalse(Node selNode)
{
Node nextNode = selNode.NextNode;
Node previousNode = selNode.PrevNode;
Node praNode = selNode.Parent;
if (praNode != null)
{
while (nextNode != null)
{
if (nextNode.Checked)
{
return;
}
nextNode = nextNode.NextNode;
}
while (previousNode != null)
{
if (previousNode.Checked)
{
return;
}
previousNode = previousNode.PrevNode;
}
praNode.CheckState = CheckState.Unchecked;
setNodeFalse(praNode);
}
}
#endregion
#region 单击树的checkbox事件 //这个是事件注意了
private void tvDevice_AfterCheck(object sender, AdvTreeCellEventArgs e)
{
Node node = tvDevice.SelectedNode;
//bool bo = node.Checked;
if (node.Checked)
{
setNodeTrue(node);
setNodeStateTrue(node);
}
else
{
setNodeStateFalse(node);
setNodeFalse(node);
}
flag = true;
}
#endregion
//直接复制使用 找到treeview AfterCheck事件绑定 tvDevice_AfterCheck方法就行
private void setNodeTrue(Node selNode)
{
Node node = selNode.Parent;
if (node != null)
{
node.CheckState = CheckState.Checked;
setNodeTrue(node);
}
}
#endregion
#region 将树的checkbox选中
private void setNodeStateTrue(Node selNode)
{
if (selNode.CheckBoxVisible)
{
selNode.CheckState = CheckState.Checked;
foreach (Node node in selNode.Nodes)
{
setNodeStateTrue(node);
}
}
}
#endregion
#region 将树的checkbox取消选中
private void setNodeStateFalse(Node selNode)
{
if (selNode.CheckBoxVisible)
{
selNode.CheckState = CheckState.Unchecked;
foreach (Node node in selNode.Nodes)
{
setNodeStateFalse(node);
}
}
}
#endregion
#region 将树的checkbox取消选中
private void setNodeFalse(Node selNode)
{
Node nextNode = selNode.NextNode;
Node previousNode = selNode.PrevNode;
Node praNode = selNode.Parent;
if (praNode != null)
{
while (nextNode != null)
{
if (nextNode.Checked)
{
return;
}
nextNode = nextNode.NextNode;
}
while (previousNode != null)
{
if (previousNode.Checked)
{
return;
}
previousNode = previousNode.PrevNode;
}
praNode.CheckState = CheckState.Unchecked;
setNodeFalse(praNode);
}
}
#endregion
#region 单击树的checkbox事件 //这个是事件注意了
private void tvDevice_AfterCheck(object sender, AdvTreeCellEventArgs e)
{
Node node = tvDevice.SelectedNode;
//bool bo = node.Checked;
if (node.Checked)
{
setNodeTrue(node);
setNodeStateTrue(node);
}
else
{
setNodeStateFalse(node);
setNodeFalse(node);
}
flag = true;
}
#endregion
//直接复制使用 找到treeview AfterCheck事件绑定 tvDevice_AfterCheck方法就行
追问
那如何实现选择相应的节点在DataGridView控件中显示相应的值呢
追答
#region getCheckBus 取得树checkbox打勾的node
private List getCheckNode()
{
List listNode = new List();
Node selectNode = tvDevice.SelectedNode;//选中的节点
Node root = null;
getRootNode(selectNode, ref root);
getAllNode(root, nodes);//取得选中节点的所有子节点
foreach (TreeNode node in nodes)
{
if (node.CheckState == CheckState.Checked)
{
busRows.Add(node);
}
}
return listNode;
}
#endregion
#region 取得根节点
private void getRootNode(Node node, ref Node rootNode)
{
Node parNode = node.Parent;
if (parNode != null)
{
getRootNode(parNode, ref rootNode);
}
else
{
rootNode = node;
}
}
#endregion
#region 取得传入节点及其所有子节点
private void getAllNode(Node node, List nodeList)
{
nodeList.Add(node);
if (node.Nodes.Count > 0)
{
foreach (Node nodeChild in node.Nodes)
{
getAllNode(nodeChild, nodeList);
}
}
}
#endregion
试试 临时写 拿到选中的节点
展开全部
这是一些节点选中的代码:可以看看
private void SetTreeNodeText(TreeNode curNode,string empLimit)
{
if (empLimit.IndexOf(curNode.Text) != -1)//包含
{
curNode.Checked = true;
}
else
{
curNode.Checked = false;
for (int k = 0; k < curNode.Nodes.Count; k++)
{
SetTreeNodeCheck(curNode, false);
}
}
if (curNode.Nodes.Count > 0)
{
for (int i = 0; i < curNode.Nodes.Count; i++)
{
string text = curNode.Nodes[i].Text;
if (empLimit.IndexOf(text) != -1)
{
curNode.Nodes[i].Checked = true;
SetTreeNodeText(curNode.Nodes[i], empLimit);
}
else
{
SetTreeNodeText(curNode.Nodes[i], empLimit);
curNode.Nodes[i].Checked = false;
selectParentNode(curNode.Nodes[i], false);
}
}
}
}
void setParentCheck(TreeNode node, bool ischecked)
{
if (node.Nodes.Count > 0)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
if (node.Nodes[i].Checked)
{
node.Nodes[i].Parent.Checked = ischecked;
setParentCheck(node.Nodes[i], ischecked);
}
}
}
}
private void SetTreeNodeCheck(TreeNode node, bool ischecked)//设置选中状态
{
if (node.Nodes.Count > 0)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].Checked = ischecked;
this.SetTreeNodeCheck(node.Nodes[i], ischecked);
}
}
}
private void selectParentNode(TreeNode node, bool check)
{
if (node.Parent is TreeNode)
{
TreeNode pnode = (TreeNode)node.Parent;
if (pnode.Checked == false)
{
pnode.Checked = check;
}
selectParentNode(pnode, check);
}
else
{
//根节点
}
}
//清空选中的节点
private void ClearSelectedNode(TreeNode curNode)
{
curNode.Checked = false;
if (curNode.Nodes.Count > 0)
{
for (int i = 0; i < curNode.Nodes.Count; i++)
{
string text = curNode.Nodes[i].Text;
if (curNode.Nodes[i].Checked)
{
curNode.Nodes[i].Checked = false;
}
GetSelectedNodeText(curNode.Nodes[i]);
}
}
}
//获取选中节点的文本
private string GetSelectedNodeText(TreeNode curNode)
{
if (curNode.Nodes.Count > 0)
{
for (int i = 0; i < curNode.Nodes.Count; i++)
{
string text = curNode.Nodes[i].Text;
if (curNode.Nodes[i].Checked)
{
limits = limits + text + "/";
}
GetSelectedNodeText(curNode.Nodes[i]);
}
}
return limits;
}
//软件设置
private void tvSoftSet_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Tag != null)
{
if (e.Node.Tag.ToString() == "0")
{
SetTreeNodeCheck(this.tvSoftSet.Nodes[0], e.Node.Checked);
}
else if (e.Node.Tag.ToString() == "1")
{
SetTreeNodeCheck(e.Node, e.Node.Checked);
}
}
}
private void SetTreeNodeText(TreeNode curNode,string empLimit)
{
if (empLimit.IndexOf(curNode.Text) != -1)//包含
{
curNode.Checked = true;
}
else
{
curNode.Checked = false;
for (int k = 0; k < curNode.Nodes.Count; k++)
{
SetTreeNodeCheck(curNode, false);
}
}
if (curNode.Nodes.Count > 0)
{
for (int i = 0; i < curNode.Nodes.Count; i++)
{
string text = curNode.Nodes[i].Text;
if (empLimit.IndexOf(text) != -1)
{
curNode.Nodes[i].Checked = true;
SetTreeNodeText(curNode.Nodes[i], empLimit);
}
else
{
SetTreeNodeText(curNode.Nodes[i], empLimit);
curNode.Nodes[i].Checked = false;
selectParentNode(curNode.Nodes[i], false);
}
}
}
}
void setParentCheck(TreeNode node, bool ischecked)
{
if (node.Nodes.Count > 0)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
if (node.Nodes[i].Checked)
{
node.Nodes[i].Parent.Checked = ischecked;
setParentCheck(node.Nodes[i], ischecked);
}
}
}
}
private void SetTreeNodeCheck(TreeNode node, bool ischecked)//设置选中状态
{
if (node.Nodes.Count > 0)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].Checked = ischecked;
this.SetTreeNodeCheck(node.Nodes[i], ischecked);
}
}
}
private void selectParentNode(TreeNode node, bool check)
{
if (node.Parent is TreeNode)
{
TreeNode pnode = (TreeNode)node.Parent;
if (pnode.Checked == false)
{
pnode.Checked = check;
}
selectParentNode(pnode, check);
}
else
{
//根节点
}
}
//清空选中的节点
private void ClearSelectedNode(TreeNode curNode)
{
curNode.Checked = false;
if (curNode.Nodes.Count > 0)
{
for (int i = 0; i < curNode.Nodes.Count; i++)
{
string text = curNode.Nodes[i].Text;
if (curNode.Nodes[i].Checked)
{
curNode.Nodes[i].Checked = false;
}
GetSelectedNodeText(curNode.Nodes[i]);
}
}
}
//获取选中节点的文本
private string GetSelectedNodeText(TreeNode curNode)
{
if (curNode.Nodes.Count > 0)
{
for (int i = 0; i < curNode.Nodes.Count; i++)
{
string text = curNode.Nodes[i].Text;
if (curNode.Nodes[i].Checked)
{
limits = limits + text + "/";
}
GetSelectedNodeText(curNode.Nodes[i]);
}
}
return limits;
}
//软件设置
private void tvSoftSet_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Tag != null)
{
if (e.Node.Tag.ToString() == "0")
{
SetTreeNodeCheck(this.tvSoftSet.Nodes[0], e.Node.Checked);
}
else if (e.Node.Tag.ToString() == "1")
{
SetTreeNodeCheck(e.Node, e.Node.Checked);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
需要自定义树控件。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询