By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 2,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

How to check if event is loaded?

P: n/a
I'm using a DataGrid and have assigned

this.DataSourceChanged += new EventHandler( DataGrid_DataSourceChanged
);

This works fine but there is one case where it doesn't. How can I
check if DataSourceChanged has anything assigned. I can add it to the
watch because it says it needs to be on the left side of a -= or +=.

Thanks,
Brett

Sep 1 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Unless you "own" the class publichsing the event, there is no robust
way of doing this. However, if you have subscribed to the event (and it
didn't error) then chances are that it worked fine and the
DataSourceChanged backer has (at least) 1 delegate.

Can you give any more details on the one case where it doesn't work? It
is more likely to be specific to your usage / expectations.

Marc

Sep 2 '06 #2

P: n/a
Unless you "own" the class publichsing the event

What do you mean by own the class? I've created all the code involved.

The event isn't firing in the one case. I know because a breakpoint in
the delegated method is never hit in the one case. I'd like to give
details but it would require lots of explaining. If there is just a
way to check the invocation list for the one event, that would be a
good start.

Thanks,
Brett

Sep 2 '06 #3

P: n/a
I mean DataGrid, so no: you don't. And no there isn't.

Sep 2 '06 #4

P: n/a
You could always add a public method call that returned the delegate list to
the class the defines the event. You could then examine the delegates to see
if the one you think ought to be there really is.

public Delegate[] GetlList()
{
if ( YourEventNameHere != null )
return YourEventNameHere.GetInvocationList();
return new Delegate[0]; // empty list
}
"Brett Romero" <ac*****@cygen.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>Unless you "own" the class publichsing the event

What do you mean by own the class? I've created all the code involved.

The event isn't firing in the one case. I know because a breakpoint in
the delegated method is never hit in the one case. I'd like to give
details but it would require lots of explaining. If there is just a
way to check the invocation list for the one event, that would be a
good start.

Thanks,
Brett

Sep 3 '06 #5

P: n/a

David Levine wrote:
You could always add a public method call that returned the delegate list to
the class the defines the event. You could then examine the delegates to see
if the one you think ought to be there really is.

public Delegate[] GetlList()
{
if ( YourEventNameHere != null )
return YourEventNameHere.GetInvocationList();
return new Delegate[0]; // empty list
}
DataGrid.DataSourceChanged doesn't have a GetInvocationList().

Thanks,
Brett

Sep 3 '06 #6

P: n/a
You said that you wrote the event but I gather that is not the case.

If it's an event then either it has a GetInvocationList() method or they
defined their own backing store for the delegates. In either case it is only
directly accessible to the class that defines the event, not to subscribers
to the event. But you can use reflection to access those hidden fields. I
suggest using reflector to look at how the event was implemented. You may
able to use reflection to access the delegate list.

"Brett Romero" <ac*****@cygen.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>
David Levine wrote:
>You could always add a public method call that returned the delegate list
to
the class the defines the event. You could then examine the delegates to
see
if the one you think ought to be there really is.

public Delegate[] GetlList()
{
if ( YourEventNameHere != null )
return YourEventNameHere.GetInvocationList();
return new Delegate[0]; // empty list
}

DataGrid.DataSourceChanged doesn't have a GetInvocationList().

Thanks,
Brett

Sep 3 '06 #7

P: n/a

David Levine wrote:
You said that you wrote the event but I gather that is not the case.

If it's an event then either it has a GetInvocationList() method or they
defined their own backing store for the delegates. In either case it is only
directly accessible to the class that defines the event, not to subscribers
to the event. But you can use reflection to access those hidden fields. I
suggest using reflector to look at how the event was implemented. You may
able to use reflection to access the delegate list.
I inherited DataGrid for my own DataGrid. DataSourceChanged is an
event that comes from the base (DataGrid). I only created a method in
my datagrid to delegate the event to. I didn't write the handler.

I've added the datagrid in question to my watches. I can see
InvocationList deep down inside the datagrid. However, the only thing
ever there is DoubleClick. So I still don't see a route to figure out
if the DataSourceChanged event is wired up according to the datagrid.

Thanks,
Brett

Sep 3 '06 #8

P: n/a
Let's try a different approach...you said previously that you subscribed to
the DataSourceChanged event. Check the invocation list of that event
immediately to ensure yourself that you did indeed subscribe to it. Now, why
would you think that at some point later it is not subscribed? Are you sure
that the conditions that should cause the event to get fired have actually
occurred?

"Brett Romero" <ac*****@cygen.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
>
David Levine wrote:
>You said that you wrote the event but I gather that is not the case.

If it's an event then either it has a GetInvocationList() method or they
defined their own backing store for the delegates. In either case it is
only
directly accessible to the class that defines the event, not to
subscribers
to the event. But you can use reflection to access those hidden fields. I
suggest using reflector to look at how the event was implemented. You may
able to use reflection to access the delegate list.

I inherited DataGrid for my own DataGrid. DataSourceChanged is an
event that comes from the base (DataGrid). I only created a method in
my datagrid to delegate the event to. I didn't write the handler.

I've added the datagrid in question to my watches. I can see
InvocationList deep down inside the datagrid. However, the only thing
ever there is DoubleClick. So I still don't see a route to figure out
if the DataSourceChanged event is wired up according to the datagrid.

Thanks,
Brett

Sep 4 '06 #9

P: n/a

David Levine wrote:
Let's try a different approach...you said previously that you subscribed to
the DataSourceChanged event. Check the invocation list of that event
immediately to ensure yourself that you did indeed subscribe to it. Now, why
would you think that at some point later it is not subscribed? Are you sure
that the conditions that should cause the event to get fired have actually
occurred?
At the point I'm checking on the subscriber side, here is what I do:

MyDataGrid.DataSource = null;
MyDataGrid.DataSource = myDataTable;

I do that on purpose to make sure the source is not null and ensure
DataSourceChanged() has to fire. Inside of MyDataGrid, I override the
datasource and assign the value coming in to base.datasource. After
that assignment, DataSourceChanged() fires. It all works fine except
for this one condition on the above code. I have a breakpoint inside
of MyDataGrid.DataSourceChanged() method as well. I'll go through the
above code a few times and all works fine. Then I hit a point at which
it doesn't fire my breakpoint inside of DataSourceChanged. Hope that
helps more.

Now, how exactly do I get at InvocationList() for DataSourceChanged?
Where do I check that exactly? Are you saying at some place I break
and check MyDataGrid.InvocationList()? Because if you are, that method
doesn't exist there. It's buried down inside of base, base on the
watch list for my custom datagrid.

Thanks,
Brett

Sep 4 '06 #10

P: n/a
And you are sure that myDataTable isn't null?
Are there any threads involved? (since the DataGrid is a winform
control, it could create issues if you are changing the property on the
wrong thread).

Inside the DataSource setter, it checks:

if ((this.dataSource == null) || !this.dataSource.Equals(value))
{
if (((value == null) || (value == Convert.DBNull)) &&
((this.DataMember != null) && (this.DataMember.Length != 0)))

{blah}

if second "if" returns true, ir runs simple code (and doesn't fire
event); if it returns false (real data source) it calls:

this.EnforceValidDataMember(value); // only if value not null
this.ResetParentRows();
this.Set_ListManager(value, this.DataMember, false);

It is this last that fires the event, but it is very complex. Could any
of this explain your issue? Is the data really valid? Is it non-null?
On the right thread? etc.

Marc

Sep 4 '06 #11

P: n/a

Marc Gravell wrote:
And you are sure that myDataTable isn't null?
Are there any threads involved? (since the DataGrid is a winform
control, it could create issues if you are changing the property on the
wrong thread).

Inside the DataSource setter, it checks:

if ((this.dataSource == null) || !this.dataSource.Equals(value))
{
if (((value == null) || (value == Convert.DBNull)) &&
((this.DataMember != null) && (this.DataMember.Length != 0)))

{blah}

if second "if" returns true, ir runs simple code (and doesn't fire
event); if it returns false (real data source) it calls:

this.EnforceValidDataMember(value); // only if value not null
this.ResetParentRows();
this.Set_ListManager(value, this.DataMember, false);

It is this last that fires the event, but it is very complex. Could any
of this explain your issue? Is the data really valid? Is it non-null?
On the right thread? etc.

Marc
Marc,

First, where are you getting all of these details about how the
datasource operates?

The data is there in myDataTable. I see a table name when mousing over
it and when looking at the data debug view (clicking the magnifying
class), I see all of the columns and rows of data. This is a single
threaded app.

this.datasource is null because of the first null assignment I do.
value is valid with data and the two are different. All of the
conditions are correct to trigger the event. DataMember is not being
used.

It's to much explaining without walking through the code but here's a
little info on the overall picture:
The form in question (FormA) is in an MDI app. Double clicking a row
in the datagrid opens FormB in the MDI. The user can make an update on
FormB and click an update button. This fires an event that FormA is
listening for and reassigns the datasource of the grid on FormA.

When the app initially loads, FormA fills with data automatically.
Everything works fine. However, when the user closes FormA (not the
app) and reopens via the toolbar new button, the update event fires but
not the DataSourceChanged event. DataSourceChanged does fire however
for other events (for both paths to FormA). There is no difference
here. Loading FormA via new app initialization or clicking the new
button uses the same method (ShellForm.MethodA) to load the form. But,
getting to FormA in those two ways is the only difference I have found,
which doesn't say anything since nothing happens to FormA until you are
in ShellForm.MethodA.

Thanks,
Brett

Sep 4 '06 #12

P: n/a
First, where are you getting all of these details about how the
datasource operates?
Reflector; quite simply one of the most powerful and useful .Net tools I
know of (outside of the framework itself):

http://www.aisto.com/roeder/dotnet/

Reading your description, it sounds like everything should work. I still
strongly suspect that there is a gotcha in your code somewhere; I can only
really suggest trying to reproduce it in a simple example. Sorry, but I'm
out of ideas without some source code.

Marc
Sep 4 '06 #13

P: n/a
I see beyond what I'm doing in DataSource into the base.DataSource,
which you posted above. However, I can't step into it to see what
conditions are being met. Do you have any suggestions?

Here's my datasource:

public virtual Object DataSource
{
get
{
return base.DataSource;
}
set
{
base.DataSource = value;
//base.DataSource = value
if( ResultsCountChanged != null && base.DataSource !=
null )
ResultsCountChanged( new
CustomDataGridResultCountEventArgs( ( ( DataTable )this.DataSource
).DefaultView.Count ) );
}
}

Thanks,
Brett

Sep 5 '06 #14

P: n/a
I think I found something in the datagrid:

base.Events.parent.events.head.next.next.handler.M ethod

it's value is

Void CustomDataGrid_DataSourceChanged(System.Object, SystemEventArgs)

I have it in the watch. There are a large number of items to view on
this. However, I'm not sure what exactly in there I should look for.
Any suggestions?

Thanks,
Brett

Sep 5 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.