如何基于树视图选择在datagridview中选择特定行 [英] How to make the particular row get selected in datagridview based on treeview selection

查看:150
本文介绍了如何基于树视图选择在datagridview中选择特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将有一个带有某些节点的treenode.我的表单上将有一个datagridview.最初,我将一些数据加载到gridview中.现在,如果我在trreview中选择一个节点,我想将特定的行作为所选行.

假设我的树视图如下

根|->子|-> Child1

如果我选择child,那么我想在选择child1的情况下进行相应的选择.


我的代码如下所示

I will have a treenode with some nodes. I will have a datagridview on my form. Initially i will load some data in to the gridview. Now if i select a node at my trreview i would like to make a particular row as selected one.

Suppose my treeview is as follows

Root |-> Child |->Child1

If i select child i would like to make a corresponding row as selected if child1 another row should get selected.


I write my code as follows

if (tvwACH.SelectedNode.Parent != null)
       {
           string str = tvwACH.SelectedNode.Index.ToString();
           if (str == "0")
           {
               Int16 tag = Convert.ToInt16(str);
               this.dataGridView1.Rows[tag].Selected = true;
           }



但是每个节点的索引都变为0.所以请提供任何帮助



But the index for every node is getting 0. so any help please

推荐答案

首先,如何维护节点与datagridview行之间的关系?是否有绑定对象或??

要利用您的代码,我要做的是:填充树形视图时,我将使用treenode的"Tag"属性(如
)来跟踪相应的索引
first of all how do you maintain the relationship between a node and the datagridview row? is there like a bound object or ??

to take advantage of your code, what I would do is: when populating my treeview I would keep track of the corresponding index using the "Tag" property of the treenode like
//in your load function
TreeNode node=tvwACH.Nodes.Add("node text");
node.Tag=row_index;



然后修改当前代码即可.



then by modifying your current code it would be.

if (tvwACH.SelectedNode.tag != Null)
{
    Int16 tag = Convert.ToInt16(tvwACH.SelectedNode.tag);
    this.dataGridView1.Rows[tag].Selected = true;
}



希望这对您有所帮助.如果您可以发布如何填充树视图和网格视图,则可能更容易澄清.



hope this helps.if you could post how you populate the treeview and the gridview, it might be easier to clarify this.


与其标记行的索引,不如标记行您可以标记行本身.
Instead of tagging the row''s index, why don''t you tag the row itself.
for(int i=0; i<treeview1.nodes.count;>{
    TreeNode node = treeView1.Nodes[i];
    DataGridViewRow row = dataGridView1.Rows[i];
    node.Tag = row;
}


假设两者具有相同数量的节点/行
接下来,处理TreeView的AfterSelect


This assumes that both have the same number of nodes / rows
Next, handle the AfterSelect of the TreeView

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    // Retrieve data from the current TreeNode
    TreeNode node = e.Node;
    DataGridViewRow row = (DataGridViewRow)node.Tag;
    row.Selected = true;
}




这应该比保留索引更好.希望对您有帮助




This should work better than keeping the index. I hope this helps


这篇关于如何基于树视图选择在datagridview中选择特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆