Solved

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

Posted on 2006-06-13
6
1,555 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

13 Experts available now in Live!

Get 1:1 Help Now