Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing a project in Visual Studio 2010
.Net Framework 4.0
Problem:
I Have two forms. Form1 contains a datagridview which is filled by students Information. next to datagridview there is button. after I select a row from datagrid and I click on the button. then my Form2 will popup and it will contains the details which are from Form1's Datagridview's Selected row. here in Form2 I can Edit the details and I can update. it will changes in my sql database as well. up to here everything is fine. but when I close my Form2 I want to refresh my Form1's datagridview automaticaly. its not happening. I tried in many ways. no solution yet. Please Give me an Idea to do it correctly. Thanks in Advance.

Help me Please!
Posted
Updated 9-Sep-13 23:35pm
v2

Hello ,

After closing Form2 , you have to call Form1_Load(object sender, EventArgs e) event by this way

Form1_Load(null,null);


and It will upate your datagridview automatically.


Regards
Animesh
 
Share this answer
 
First of all, normally controls like DataGridView don't need "refresh". You just change the data in the cells, and the view is "refreshed". (This is so because properties can have side effects programmed in their setters.)

Secondly, there are no effective parent-child relationships between forms (it can still be used, but it is not recommended). If one form is the Owner of another, this is absolutely different property, which you, by the way, should always use: it supports the integrity of the views of the same application. Besides, one form is a main one, the one used as a parameter in Application.Run. Anyway, these relationships have nothing to do with your application.

As to your particular problem, 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
thasneemjambi 10-Sep-13 2:11am    
I am doing this project as my academic one. since this is academic one I cannot do it in my wish. they are telling the flow. and according to that only I want to work. so they are telling edit the details in new form and not in same datagridview row. so how can I refresh back form datagridview. Thanks!
Sergey Alexandrovich Kryukov 10-Sep-13 2:19am    
First of all, your note is irrelevant to my answer. I just explain to you how to do required form collaboration. If you did not get some part of it, ask a follow-up question.

(And, by the way, I am sure your understanding of the academic work is totally wrong. You should be the real author of your work, unlike, many cases of, say, industrial work...)

—SA
thasneemjambi 10-Sep-13 3:09am    
Please Help Me Sir
Sergey Alexandrovich Kryukov 10-Sep-13 10:42am    
Sure, but help with what? Can you do the operation with grid view on the same form? If you can, you can implement it on this form, and implement some interface which does it. Than you can pass the reference to this interface to other form(s), then that other form will be able to call interface method(s)/property(ies) to modify the first form...
—SA
Hi
i think your problem is with automatic part(Am i right?)

if i am your answer is simple.
you should just use ShowDialog to show the form2 with your button.
the use of this code is that when you close form2 the codes under the ShowDialog are running so you just need to write your fetch data code that use them to connect to the sql data base and get the new data under the show dialog.
 
Share this answer
 

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