I just want to know this code of writing to refresh a datagridview when closing a sub form is correct or not…
I have written a function as follows
public void PerformRefresh()
{
Form2_Load(this, EventArgs.Empty);
}
In my form2 load I wrote the necessary code that needed to bind the data for datagridview. i just want to know is this the correct way or there is any better way.
No I do not think this is the correct approach; I would create a private method called
loadData()and put a call to this method in bothForm2_Load()andPerformRefresh()your approach would work but what if
Form2_Loadalso does other things which you do not want to execute at every refresh?In general we should avoid calling event handlers manually like that, even if passing
thisandEventArgs.Emptymakes those event handlers to work, they should really only be called by the .NET Framework in my opinion, you end up with nicer and easy to maintain code in this way 😉