Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-06-13
6
Medium Priority
?
1,566 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 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