Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I have a problem with passing value between usercontrols. I create two usercontrols: one usercontrol inculde treeView and one include datagridview.I would like to do: click a node from TreeView -> datagridview will load data which filter by node of treeView.

Thank you for your reply.
Posted

Use properties to pass values between controls / classses.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Mar-11 0:49am    
A 5, but what OP did not understand? may your Answer won't be understood as well. It's something like not understanding what is a class, instance and Control.
--SA
Albin Abel 23-Mar-11 1:16am    
OP asked you a question. You replied with answer. so My 5 for your property.
To elaborate on Abhinav's answer, you need to create a Property in your form with the DataGridView that holds your node value. In the Setter for the property you could call a method that filters your results and rebinds the DataGridView. Something like this :

C#
private string nodeValue;

   public string NodeValue
   {
       get
       {
           return nodeValue;
       }
       set
       {
           if (nodeValue != value)
           {
               nodeValue = value;
               NodeValueChanged(value);
           }
       }
   }

   public frmDataGridView(string nodeValue)
   {
       this.NodeValue = nodeValue;
   }

   void NodeValueChanged(string newNodeValue)
   {
       //get new results with newNodeValue as filter
   }

Then in your form with the TreView you need to create a private variable that references your GridView form, then whenever the treeview selection changes, in the treeview selection changed event you just need to change the property like this:

C#
private frmDataGridView gridView;

   public frmTreeView()
   {
       gridView = new frmDataGridView();
       gridView.NodeValue = "DefaultValue";
   }

   void TreeViewChanged(object sender, TreeViewChangedEventArgs e)
   {
       gridView.NodeValue = "Selected TreeView Value";
   }


Hope this helps.
Disclaimer. This code has not been checked, the TreeViewChanged event I have used is fictitious, so you need to use the proper TreeView changed event.
 
Share this answer
 
v3
Comments
meobien 24-Mar-11 5:47am    
I did find a solution for the problem. Thank you very much for your support. Cheers,
Hope this[^] might help you.
 
Share this answer
 
Comments
meobien 23-Mar-11 1:01am    
thanks your reply. but it is right for the first tim loader. When I click another node of TreeView, the gridview do not change.

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