?
Solved

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

Posted on 2006-06-13
6
Medium Priority
?
1,564 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

801 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