Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a problem with a project..

I have one form where I list all the customers from database in a datagridview.
The Id is autogenerated from the database. At this form with the datagrid I have a button for updating a record,when users press this button the second form shows up ready to change any field of the record. So far so good

But, at the second form I want to save the changes and i need the Id, but anything I have tried the Id comes with value 0...
Posted

In essence, 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 solution for more detail: How to copy all the items between listboxes in two forms[^].

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:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Comments
Abhinav S 24-Oct-13 23:31pm    
5!
Sergey Alexandrovich Kryukov 24-Oct-13 23:58pm    
Thank you, Abhinav.
—SA
zapata287 25-Oct-13 4:39am    
thank very much!5
Sergey Alexandrovich Kryukov 25-Oct-13 9:53am    
Thank you.
—SA
Expose a property (for auto id) in the second form.
Access it in the first form and set a value in the second form to true.
 
Share this answer
 
Comments
zapata287 24-Oct-13 15:20pm    
in the first form I can see the value of Id. I wrote a method to return the Id and from the second form forst I create an instance of the first form and then I try to get the value through the above method but the value is zero and not the real value.

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