What is the best way to inform a second form that it's data has been changed

T

Tony Johansson

Hello!

I have two forms each containing a DataGridView that has a textFile as it's
data.
When I want to save the DataGridView to the textfile I push the save button.

If I for example change the DataGridView in one of the form and then push
the save button
how do I best inform the other form that it has to reload the DataGridView
because the data(textfile)
has been changed.

Somebody might have a simple example for this kind of thing.


//Tony
 
C

Cor Ligthert[MVP]

Tony,

The DataTable has strange enough no method "haschanges",

However you know if the method

DataRow[] drs = DataTable.GetChanges();
returns null then it has no changes.

Cor
 
T

Tony Johansson

Hello!

I mean to inform the other form that it has to relead the DataGridView from
the textfile is the best way here to use delegate.

//Tony


Cor Ligthert said:
Tony,

The DataTable has strange enough no method "haschanges",

However you know if the method

DataRow[] drs = DataTable.GetChanges();
returns null then it has no changes.

Cor

Tony Johansson said:
Hello!

I have two forms each containing a DataGridView that has a textFile as
it's data.
When I want to save the DataGridView to the textfile I push the save
button.

If I for example change the DataGridView in one of the form and then push
the save button
how do I best inform the other form that it has to reload the
DataGridView because the data(textfile)
has been changed.

Somebody might have a simple example for this kind of thing.


//Tony
 
A

Alberto Poblacion

Tony Johansson said:
I mean to inform the other form that it has to relead the DataGridView
from the textfile is the best way here to use delegate.

I'd suggest using an event. On the form that must inform the other one
that the data has changed, expose a public event and raise it when changing
the data. The form that needs the notification can suscribe to the event and
reload the data in the event handler routine.
 
T

Tony Johansson

Hello!

But if I will use an event I must create a delegate because the type of the
event is the definition of the delegate.

//Tony
 
B

Ben Voigt [C++ MVP]

Tony Johansson said:
Hello!

I have two forms each containing a DataGridView that has a textFile as
it's data.
When I want to save the DataGridView to the textfile I push the save
button.

If I for example change the DataGridView in one of the form and then push
the save button
how do I best inform the other form that it has to reload the DataGridView
because the data(textfile)
has been changed.

If you want to reload when the textfile changes, then you'll need
FileSystemWatcher. Textfiles can be changed by any program, not just yours.
 
B

Bill Yanaire

Tony Johansson said:
Hello!

I mean to inform the other form that it has to relead the DataGridView
from the textfile is the best way here to use delegate.

//Tony
Here is an idea. How about a global flag. Once you change the data, you
set the flag to TRUE. In your second form, just read the flag. Simple.
Works.
 
T

Tony Johansson

Hello!

Ben's suggestion will hold perfectly

//Tony

Peter Duniho said:
[...]
If I for example change the DataGridView in one of the form and then
push the save button
how do I best inform the other form that it has to reload the
DataGridView because the data(textfile)
has been changed.

If you want to reload when the textfile changes, then you'll need
FileSystemWatcher. Textfiles can be changed by any program, not just
yours.

Or as an alternative viewpoint: if you are using a text file to
communicate between two forms, and expect for that text file to be changed
by anything else, there is probably a problem with that particular design.

In other words, if you feel that Ben's advice isn't useful, there's
probably something wrong with the approach at a more fundamental level.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top