Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
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;
           }


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

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.

C#
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.
 
Share this answer
 
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;
}

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900