Developer
Developer

Reputation: 8636

refresh datagridview when closing child form?

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.

Upvotes: 0

Views: 527

Answers (1)

Davide Piras
Davide Piras

Reputation: 44605

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 both Form2_Load() and PerformRefresh()

your approach would work but what if Form2_Load also 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 this and EventArgs.Empty makes 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 ;-)

Upvotes: 1

Related Questions