Home > OS >  Tree-views in table layout panel and get values dynamically c#
Tree-views in table layout panel and get values dynamically c#

Time:01-11

I have multiple treeviews in the table layout panel - C# windows application.

Each cell consists of Treeview or dropdown or textbox. I can get the value of textbox and dropdown but I am unable to get the selected node of multiple Treeviews in the table layout panel.

below my code.

        int rows;
        int column;

        rows = tableLayoutPanel1.RowCount;
        column = tableLayoutPanel1.ColumnCount;

       


        for (int i = 0; i < rows; i  )
        {
            for (int j = 0; j < column; j  )
            {
                Control value = tableLayoutPanel1.GetControlFromPosition(j, i);
                //here i got specified treeview but i can't get refernce. getting error
                string controlName = value.Controls.Owner.Name;
                //here i got error i am unable to get treeview selected text
                string seletedvalue = controlName.SelectedNode.Text;
                MessageBox.Show(controlName);
            }
        }

CodePudding user response:

You are getting the control, but you are not checking what type of control it is, and you are not casting it. So it stays as a general control and not a TreeView. And only the TreeView has nodes.

You can also simply the selection of the specific control that you want.

foreach ( TreeView tv in tableLayoutPanel1.Controls.OfType<TreeView>()) 
{     
    string seletedvalue = tv.SelectedNode.Text;
    MessageBox.Show(tv.Name   " "   seletedvalue);
}
  •  Tags:  
  • Related