trailerman
trailerman

Reputation: 321

Refresh Data in ComboBox List

I have a datatable (jobs) with a column in this table called StatusId. I have another datatable (jobStatus). The job table has 1 job record and the jobstatus table has all the job status.

On my winforms form I have the job record displayed and the job status is a combobox displaying the contents of the jobstatus table. The List portion (DisplayMember) is bound to the jobstatus table and the data portion is bound to the jobs table. (ValueMember) So all works well and when my jobs are selected and displayed the combobox selects the respective job status...All good.

Now I have another form (JobStatus) where I can added more job status records. So while I am entering / changing job records I discover I need another job status, so I jump over to my job status form and enter my new status. I then jump back to the job form and I now want to be able to select my new job status in the combobox.

My questions is what is the best practice for this senario where new records have been added or edited to a lookup style list which is used to populate a combobox on another form. I have tried putting code in the activate event of the form which works except the form flickers and it looks a ugly.

Any ideas???

I was virtually doing what your said, which made me look a bit closer at the code. The issue was the order I was processing the lines of code, and I found that when

old code

  cboCustomer.DataSource = Business.Contact.GetContact( Enums.ContactType.Customer ).Tables[0];
            sorted = ( ( DataTable ) cboCustomer.DataSource ).DefaultView;
            sorted.Sort = "Name ASC";

new code here

  DataView dv = Business.Contact.GetContact( Enums.ContactType.Customer ).Tables[0].DefaultView;
            dv.Sort = "Name ASC";
            DataTable dt = dv.ToTable();
            cboCustomer.DataSource = dt;

works like a charm. Cheers for your help

Upvotes: 0

Views: 200

Answers (1)

Cody Gray
Cody Gray

Reputation: 244782

You should be able to use the Activated event without a problem. That's even what the documentation says it is good for:

You can use this event for tasks such as updating the contents of the form based on changes made to the form's data when the form was not activated.

Of course, you'll get a few false positives when you subscribe to this event on the parent form. Consider the case where the user switches to the form without attempting to use the combo box control. It's not a huge deal, but in that case you will have updated the combo box for no reason. This is easily fixed by switching to handling the Enter event for the combo box control. This one only gets raised when that specific control receives the focus. The only limitation of the Enter event is that you can't change the focused control from inside of this event handler (but you don't want to do that anyway).

So now we need a fix for the flicker problem, which I think you'll find to be pleasantly simple. At the beginning of the event handler method (regardless of which one you use), before you start updating the combo box, call its BeginUpdate method. This prevents the control from flickering while it is being updated (by suppressing painting). At the end, you need to finish with a call to EndUpdate, which reenables painting and performs a single repaint to reflect your changes.

I just tested this myself, and I don't see any flicker. The most you'll get is a single flicker while the control gets repainted to reflect your changes. Obviously, you can't do any better than that.

Upvotes: 1

Related Questions