Solved

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

Posted on 2006-06-13
6
1,558 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now