Ajay_Kumar
Ajay_Kumar

Reputation: 1387

Form reloading in C#.net

I have a form i am fetching many records from database that are rendering dynamical on from. When i am clicking on Delete button this is deleting as well but I want to re-render my form so user will feel that records is deleted completely. when i am writing code this.refresh(); this is not fetching values from database and i am not seeing record is completely gone. It is windows C# form.

Upvotes: 0

Views: 10335

Answers (3)

Ran
Ran

Reputation: 6159

The methods Form.Refresh and Form.Invalidate have nothing to do with causing the application to retrieve new data from the database.

  • Form.Refresh - redraws the form synchronously (i.e. Form.OnPaintBackground and Form.OnPaint are called directly on the current thread).

  • Form.Invalidate - redraws the form asynchronously (i.e. WM_PAINT message is sent to the window, so Form.OnPaintBackground and Form.OnPaint will be called by the UI thread when it handles messages).

In order to update records, you need to make your control retrieve updated data.

If you specify more details on how the Form pulls that data, someone can help you figure out how to update it. For example, are you using data binding? Did you write your own code to retrieve the records?

Basically, without seeing your code, I would say you can take whatever it is you are doing in the constructor and put it in a separate method (let's call it RefreshData). Then, call RefreshData after you delete records from the database.

So, you're creating new controls that represent the data. In that case, there's no mechanism to automatically do the updating work for you, you'll have to code it. You generally have two options:

  1. Keep a reference to the controls you created, and remove them from the Form before updating it.

  2. Implementing something more efficient, like maintain a Dictionary that maps from the Data Rows to the controls that represent it, so that you can go over the updated data, and for each row in your dictionary that disappeared from the data, remove the controls.

You may also want to consider using some control that has a built-in Data Binding mechanism that would save you lots of work. For example, a GridView.

Upvotes: 2

dzendras
dzendras

Reputation: 4751

If you are filling the form in constructor you need to extract loading code to a method. Then call it on DeleteButton.OnClick event after performing delete.

Upvotes: 0

ColinCren
ColinCren

Reputation: 615

Could you be looking for Invalidate() ?

Upvotes: 0

Related Questions