简体   繁体   中英

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.

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 (ie Form.OnPaintBackground and Form.OnPaint are called directly on the current thread).

  • Form.Invalidate - redraws the form asynchronously (ie 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 .

您可以寻找Invalidate()吗?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM