Click here to Skip to main content
15,741,752 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two Forms Form 1 contains DataGridView with a Column for Town ID and in Form 2 i have a ComboBox that i already fill it with Town's names from a DataTable as DataSource with Display Member (TownName)and Value Member (Town Id).
My problem is i create in Form 2 a getter and setter for Selected Value Property of that ComboBox to assign to it the Town Id of the selected row in DataGidView from Form 1,so in button click event in form 1 i give the column value to that setter then i open form 2 to show the selected town in ComboBox but nothing happens the ComboBox doesn't receive that value, is what i did correct? or there is another method to solve my issue?
Thank you all
Sergey Alexandrovich Kryukov 18-Mar-15 8:32am    
Okay, you screwed up the thing, but how can we know how? Did you use the debugger?
[no name] 18-Mar-15 11:01am    
I set the property o selected value like this :
public int cmbVilleValue
get { return Convert.ToInt32(cmbVille.SelectedValue); }
set { cmbVille.SelectedValue = Convert.ToInt32(value); }
and then in the other Form i assign tha value after creating an instance of first form of course:
frmClientEdit.cmbVilleValue = Convert.ToInt32(dgvClient.CurrentRow.Cells["CODEVILLE"].Value);
When Debugging i found that frmClientEdit.cmbVilleValue = 0 that means no data transfered here

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form.

Please see my past answer: How to copy all the items between listboxes in two forms[^].

This is especially convenient if you use partial declaration for form classes. Note that if you have the class in different parts (which can be in different files), you don't need to show all the inheritance list. In one part, say, you add the base class, in another part you don't need it. When you implement some interface, add it in the inheritance list of one part of the class (and nothing more), and implement this interface in the same part.

Pass the instance of this form as the interface interface to another form. That form can now call members of the first form through this interface. In more rare cases, you can implement two interfaces in two collaborating forms and pass the interface references to each one to both. I hope you got the idea.

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:[^],[^].

[EDIT #] First link fixed.

Share this answer
[no name] 18-Mar-15 11:18am    
Thank u but the page related to that link u give me not found, sound it not existe anymore
Sergey Alexandrovich Kryukov 18-Mar-15 11:30am    
That is sad: good answers go. I'll remove the link. Is my solution clear without it or not?
I added detail to the answer to compensate for that.
Maciej Los 18-Mar-15 12:01pm    
Related page is now available. In case if not, see:
Sergey Alexandrovich Kryukov 18-Mar-15 12:07pm    
Thank you for the advice. I already replaced the first link with some extra text.
I also added the link you found; thank you for this valuable help.
Maciej Los 18-Mar-15 14:58pm    
You're very welcome, Sergey ;)
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

Share this answer
Maciej Los 23-Mar-15 2:50am    
Sergey Alexandrovich Kryukov 23-Mar-15 3:18am    
Thank you, Maciej.
Share this answer
Sergey Alexandrovich Kryukov 18-Mar-15 11:10am    

This article is misleading in certain aspects: non of the forms are "children" of any other forms. In fact, the problem of form collaboration does not depend on any predefined form-to-form relationships at all, it's all about just the sequence of operation and what members are added to the form to "known" about each other.

Please see my answer.

Maciej Los 18-Mar-15 11:45am    
Sergey, thank you for your comment. As per my understanding, OP wants to pass data integer value to another form and to set combobox SelectedValue property. He can still use method provided by OriginalGriff to transfer data from one form to another.
Sergey Alexandrovich Kryukov 18-Mar-15 11:58am    
Even if he can use the method itself, the article is still misleading.
Maciej Los 18-Mar-15 12:10pm    
Got your point, Sergey, but i have to partially disagree. It depends on method how the second form (let's call it "child form") is created and called. OP wrote: "i assign that value after creating an instance of first form", but we know nothing about used method.
Sergey Alexandrovich Kryukov 18-Mar-15 14:46pm    
Yes, sure, I did not mean otherwise... it's just can be different, pretty apparent scenario...

I would put the design in dependency on the form's roles.

As to the wrong term "child form"... there are just "main form" (which can also be changes, and this is a popular topic on this forum, but this is the form which was passed to currently executing Application.Run(Form)) and "other forms". This is a typical case, but the forms could be both "other forms", some could be modal, whatever... It does not change the simple principles.

ok Guys i solved the problem i used a public static variable to store the column's value and i assigned that variable to combobbox1.SelectedValue instead of method property and it works fine thanks all of u :)
Share this answer
Maciej Los 18-Mar-15 15:01pm    
Dear Youssef, i'm sad because you learned nothing today... You choosed the worse solution as possible. ;(
[no name] 18-Mar-15 15:14pm    
i Tried with public method property that i set in form 2 that contains combobox as follow :
public int cmbVilleValue
get { return Convert.ToInt32(cmbVille.SelectedValue); }
set { cmbVille.SelectedValue = Convert.ToInt32(value); }
then in the first form i got the colummn's value from selected row and assign it to this method but it didn't work.
What shoud i do mr Maciej Los me too i want to do things in right way?
Sergey Alexandrovich Kryukov 18-Mar-15 15:17pm    
This is a very bad idea. Static members are best avoided. In this case, you need to put the reference to one form instance in other form instance. Where is anything static?
[no name] 18-Mar-15 15:32pm    
i'm trying right know but always i get 0 as value instead of what i choose in datargrid

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