views:

76

answers:

4

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.

Any idea please

+1  A: 

1) you need to map the nodes to corresponding datagrid rows

this.dataGridView1.Rows[0].Tag = id; // a node id

2) handle node click event and find corresponding row by id and select it

if (tvwACH.SelectedNode.Parent != null)
{
        int id  = (int)tvwACH.SelectedNode.Tag ; // make sure you've already assigned tag when creating Three nodes and data rows
        foreach(DataGridViewRow  row in this.dataGridView1.Rows)
        {
           int rowId = (int)row.Tag ;
            if(rowId == id)
            {
              row.Selected = ture;
            }
            else
            {
              row.Selected = false; //discard other rows 
            }

         }
}
Arseny
row.tag and row.selected these were all not getting
Dorababu
fixed to "DataGridViewRow" row
Arseny
int id = tvwACH.SelectedNode.Tag This raises me an error as Cannot implicitly convert type 'object' to 'int'.
Dorababu
int id = (int)tvwACH.SelectedNode.Tag
Arseny
int tag = (int)tvwACH.SelectedNode.Tag; throwing an exception as Nullreference exception was handled
Dorababu
How to assign tags for the nodes i have not assigned any tags for the treeview
Dorababu
tvwACH.Nodes[0].tag = id; do it for all nodes and corresponding rows
Arseny
Ok but i am getting the exception as i mentioned
Dorababu
A: 

I have written like this

       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 is always returning 0. so hoe to get the Id for the selected node now

My treeview is as follows if i select BatchHeader i would like to make the 2nd row as selectable field

alt text

Dorababu
you cannot get ID like that. TreeNode has a property "tag". set the Id to it when you create new node. then in that event get this ID and look up corresponding row
Arseny
Can you update my answer and give the appropriate one
Dorababu
@Dorababu I cannot allowed to update your answer but mine. see my edited answer above.
Arseny
A: 

This is the code i have written

   private void tvwACH_AfterSelect(object sender, TreeViewEventArgs e)
    {

        string node = string.Empty;
        if (tvwACH.SelectedNode.Parent != null)
        {
             node = tvwACH.SelectedNode.Text.ToString();
            if (node == "FileHeader")
            {
                int tag = Convert.ToInt16(tvwACH.SelectedNode.Tag.ToString());
                this.dataGridView1.Rows[0].Tag = tag;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    int rowId = (int)row.Tag;
                    if (rowId == tag)
                    {
                        row.Selected = true;
                    }
                }
            }

            string strSwitch = tvwACH.SelectedNode.Parent.Text;
            switch (strSwitch)
            {
                case "ACH":
                    {
                        dataGridView1.Visible = true;
                        dataGridView1.Rows.Clear();
                        node = tvwACH.SelectedNode.Text;
                        StreamReader sr = new StreamReader(node);
                        while (sr.Peek() >= 0)
                        {
                            string line = sr.ReadLine();
                            dataGridView1.Rows.Add(rectype[line.Substring(0, 1)].ToString(), line);
                        }
                        sr.Close();
                    }

                    break;

            }
        }
    }
Dorababu
this.dataGridView1.Rows[X].Tag = tag; // where x is current row you mus assind it where you add new row to datagrid and pass id taken from current(selected) node
Arseny