Detect from what form an event fired

F

fabrizio

Hello all ,

I have a small problem. My app has two forms:

On the first one (frmMain) i have a datagrid which modifies a
datatable. I can modify data and i use the datatable rowchanged event
to apply changes to my database. (I know i could use the
adpater.update method but i prefer to do it this way). Te dataset is
public so that i can access it also from other forms.

I also have a second form (frmAltro) that i load from a button click.
This form has a datagrid which gets data from the same datatable as
the one on frmMain. From this form (frmMain) i add some datarows to
the datatable when the closing event is fired. The problem is that the
rowchanged event of the dataset will fire (as it should do) when i
edit the datatable either from frmMain or frmAltro. Is there a way to
prevent the rowchanged event to fire when the datatable is edited from
frmAltro? Or maybe is there a way that i can know which form (frmMain
or frmAltro) caused the rowchanged event to fire..?

Regards,

Fabrizio Mazzoni
 
J

Javier Campos

fabrizio said:
edit the datatable either from frmMain or frmAltro. Is there a way to
prevent the rowchanged event to fire when the datatable is edited from
frmAltro? Or maybe is there a way that i can know which form (frmMain
or frmAltro) caused the rowchanged event to fire..?

Not directly, but I could suggest:

on frmMain:

/////
private bool trackRowChanged = true;
public bool TrackRowChanged { get { return trackRowChanged; } set {
trackRowChanged = value; }

private void myDataGrid_RowChanged(object sender, EventArgs e) {
if(!trackRowChanged) return;
// Do my Row Changed stuff
}
//////

on frmAltro's button:

//////
private boid myButton_Click(object sender, EventArgs e) {
MyFormMain.TrackRowChanged = false;
// Do your stuff here, including adding rows to the datagrid
Application.DoEvents(); // Fire up the RowChanged events
MyFormMain.TrackRowChanged = true;
}
///////

If this happens for a lot of grids, or is a repeated task, or you may want
it to act differently (instead of do nothing/do something), you could also
have a "control"-type property (instead of a bool one), and do:

MyFormMain.SenderForm = (control)((control)sender).Parent;

as the first line on each event that changes anything in the frmMain.

Hope this helps,

Javier Campos
 
G

Guest

Can you not determine which datagrid is sending the event from the 'object sender' part of the event and take the appropriate action?
 
J

Javier Campos

PocketNerd said:
Can you not determine which datagrid is sending the event from the 'object
sender' part of the event and take the appropriate action?

Yes, but supposedly, the datagrid is in just one form (or so I understood),
so no matter which form caused the datagrid to invoke the event, the sender
object will be the same.

Either that, or I understood it totally wrong :)

- Javier Campos
 

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