开发者

Form reloading in C#.net

I have a form i am fetching many records from database that are rendering dyn开发者_Go百科amical 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 (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.


Could you be looking for 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜