0
votes

I am working on C# Project [Windows Form Application], to update treeview nodes from excelsheet [xls] Cell [row i, Column 3] Values, while on selecting treenode, it should update corresponding Column 4 Value [row i, Column 4]. For me, Treenode are populated successfully, but on selecting the treenode, it always display first Element of treenode [Not selected one].

Populated Treenode from Excel as: [ Update Child Nodes from Column 3 elements [Column 2 Contain Parent node name and Column 3 have Child Node name], if Column 2 Value is same as Parent node name [My Module], update the child nodeunder same Parent node.]

            for (int i = 0; i < worksheet.UsedRange.Rows.Count; i++)
            {
                string mynode = ((Excel.Range)worksheet.Cells[i + 1, 3]).Value2.ToString();
                string mynode2 = ((Excel.Range)worksheet.Cells[i + 1, 2]).Value2.ToString();

                if (mynode2 == mymodule)
                {
                    TreeNode ChildNode = ParentNode.Nodes.Add(mynode);
                    ChildNode.Text = mynode;
                }                    
            }

On selecting the Child Node, it always give 1st Parent node. Instead of Selected Node.

      for (int i = 0; i < worksheet.UsedRange.Rows.Count - 2; i++)            
        {                
string mynodetext = ((Excel.Range)worksheet.Cells[i + 2, 3]).Value2.ToString();
 string mynodetext1 = ((Excel.Range)worksheet.Cells[i + 2, 4]).Value2.ToString();

        if (treeView1.SelectedNode.FirstNode.Text == mynodetext)
       {
          this.richTextBox1.SelectedText += Environment.NewLine + mynodetext1 + Environment.NewLine;
       }
      }

Please Guide, How to get correct selected Node.

1
What about trying treeView1.SelectedItem?Ben

1 Answers

1
votes

You need to use

treeView1.SelectedItem

to access the value. Just keep in mind that this is a read only property compared to listbox and combobox controls.