Solved

Refresh data on form after changes have been made in another form

Posted on 2006-06-13
6
1,559 Views
Last Modified: 2012-05-05
I have an application with role based business logic and security.  In order to simplify the application test process I created a simple application where developers and testers can change their roles.  User and roles data as well as the assignment table are stored in a SQL Server 2005 database.

If the user starts the application she/he sees a MainForm with checkboxes for each potential role a user can have. The user check marks the roles she/he needs and the data will be updated immediately.

Sometimes developers need to add new roles, change existing roles or assign roles on non-privileged users behalf. To make this possible I added a button to the MainForm which will open another form, combining all data in a datagridview. I call this form ListForm. This is my buttons click event:

private void toolStripLabel1_Click(object sender, EventArgs e) {
      ListForm list = new ListForm();
      list.Show();
}


Basically the user accesses the same data from both forms. However, while MainForm contains only a subset of the data (the roles that can be assigned to the current user) ListForm contains the data for all users.

This is how I fill the data on MainForm:
  this.applicationUserRoleTableAdapter.FillBy(this.nD_TestDBDataSet.ApplicationUserRole, userName);

This is how I fill the data on ListForm:
  this.applicationUserRoleTableAdapter.Fill(this.nD_TestDBDataSet.ApplicationUserRole);

The problem is that after returning to the MainForm the changes made in ListForm are not reflected in MainForm.  If I restart the application the changes are there.

What can I do to get the MainForm updated.  Anything I tried didn't work (e.g. CurrencManager.Refresh()), but probably I just did it the wrong way or from the wrong place.

Any suggestions are welcome.

Peter
0
Comment
Question by:pgloor
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
ID: 16894154
Hi, some possible ways:

1)
Define a boolean property such as "DataChanged" in ListForm, and check if thats true after ListForm has been closed, if so, update the data on the main form. So you may have the following in ListForm:

     //Global variable
     private bool m_DataChanged = false;
...
     //Read only property in ListForm
     public bool DataChanged
     {
         get { return this.m_DataChanged; }
     }

So when a change does occur, set m_DataChanged to true. Modify your existing form opening code to:

private void toolStripLabel1_Click(object sender, EventArgs e) {
     ListForm list = new ListForm();
     list.ShowDialog(); //Note the use of ShowDialog() method, program execution blocks here until ListForm as been closed

     if (list.DataChanged)
     {
          //Refresh data on "this" form
     }
}

2)
Define an event in the ListForm form, subscribe to the event in the mainform, fire the event when a change has occurred in ListForm and handle it appropriately in mainform.
0
 
LVL 12

Expert Comment

by:topdog770
ID: 16895295
Zephyr's given you a pretty good and easy to implement approach.

Another option is to make your data exist outside of the form(s), so while moving from one form to another, any changes made on either form persist from form to form.
0
 
LVL 5

Author Comment

by:pgloor
ID: 16895328
It drives me crazy! I tried everything you suggest. Nothing happens!!!

How, do you think, should the code look like that refreshes the data on the main form?

I tried anything I can think of.

Even reloading the data using the FillBy method of the table adapter followed by some of the Refresh() methods I found, didn't help.

0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16895468
Can you confirm that you used the ShowDialog() method to show the ListForm?
0
 
LVL 5

Author Comment

by:pgloor
ID: 16895933
In my example I mentioned:

private void toolStripLabel1_Click(object sender, EventArgs e) {
     ListForm list = new ListForm();
     list.Show();
}

Sorry, this is a typo. The example should read like this.

private void toolStripLabel1_Click(object sender, EventArgs e) {
     ListForm list = new ListForm();
     list.ShowDialog();
}


Just to make it clear...
... I tried to update the datagridview like this:

private void toolStripLabel1_Click(object sender, EventArgs e) {
  // Validate and update current changes
  this.Validate();
  this.applicationUserRoleBindingSource.EndEdit();
  this.applicationUserRoleTableAdapter.Update(this.nD_SafirDBDataSet.ApplicationUserRole);

  // Open the list form to make some changes
  ListForm list = new ListForm();
  list.ShowDialog();

  // Update datagrid with current user data
  this.applicationUserRoleDataGridView.Update();
}

... and I tried to refresh the datagridview like this:

private void toolStripLabel1_Click(object sender, EventArgs e) {
  // Validate and update current changes
  this.Validate();
  this.applicationUserRoleBindingSource.EndEdit();
  this.applicationUserRoleTableAdapter.Update(this.nD_SafirDBDataSet.ApplicationUserRole);

  // Open the list form to make some changes
  ListForm list = new ListForm();
  list.ShowDialog();

  // Refresh datagrid with current user data
  this.applicationUserRoleDataGridView.Refresh();
}

And I tried many other things, like invalidating the datagridview, from which I hoped they will do the trick.

I'm sure the data have been changed, since the correct data appear when I restart the application.



0
 
LVL 5

Author Comment

by:pgloor
ID: 16895956
Finally, either of this two works:

-----------------------------
  list.ShowDialog();
  this.applicationUserRoleTableAdapter.FillBy(this.nD_SafirDBDataSet.ApplicationUserRole, userName);
  this.applicationUserRoleDataGridView.Refresh();
-----------------------------
  list.ShowDialog();
  this.applicationUserRoleTableAdapter.FillBy(this.nD_SafirDBDataSet.ApplicationUserRole, userName);
  this.applicationUserRoleDataGridView.Update();
-----------------------------
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question