This is a migrated thread and some comments may be shown as answers.

When binding, I sometimes get "Exception has been thrown by the target of an invocation"

3 Answers 157 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 19 Nov 2008, 09:44 PM
This has happened on a couple of my WPF pages that use the GridView.  I select an item on my list which populates some textboxes.  I update those textboxes, click "Update" and then reload the GridView.  Reloading the GridView consists of re-retrieving the data and setting the ItemsSource.  On this particular page I have, it binds properly the first time, but all subsequent re-binds I get this.  Any ideas what might be happening?

3 Answers, 1 is accepted

Sort by
0
Hristo Deshev
Telerik team
answered on 21 Nov 2008, 10:09 AM
Hello Scott,

I can't say much without looking at your code, but I have a theory. Maybe you are rebinding the grid in the middle of some internal operation, say in an event handler that gets triggered by the grid. You can delay the rebinding by using Dispatcher.BeginInvoke:

this.Dispatcher.BeginInvoke((Action) delegate(){ 
    RebindGrid(); 
}); 

That might fix your problem. Try it, and tell me if it works for you.

Of course there is the possibility that you have hit a bug or a limitation with our product. Just to get to the bottom of this, can you provide more details on how you bind the grid, or maybe post a code snippet that will let me reproduce the exception? Thanks in advance.

I am attaching a sample project that uses the Dispatcher technique I described.


Regards,
Hristo Deshev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Scott
Top achievements
Rank 1
answered on 03 Dec 2008, 03:05 PM
I found the issue.  On the first binding of my radGridView, it works perfectly.  After I have selected a record from the grid, populated some textboxes for editing, and then made changes to the data, I then update the database and then rebind the grid (to refresh it). 

On the initial bind, it does not fire the SelectionChanged event, but on the re-bind, it does.  In my SelectedChanged event I reference the SelectedItem of the grid.  It blows up right there with an exception that says "Object reference not set to an instance of an object".  So I either have to check to see if a SelectedItem exists or use another event like MouseLeftButtonDown. 

I don't understand why it raises the SelectionChanged event on the re-bind, but I at least have a way to work around it.


0
Accepted
Nedyalko Nikolov
Telerik team
answered on 05 Dec 2008, 01:08 PM
Hello Scott,

SelectionChange event raises both when the item is selected or unselected. When you rebind the grid this is equivalent to "unselecting" the previously selected item. This is actually a common behavior and it's natural for all selection controls (ListBox, ListView etc.).

Regards,
Nedyalko Nikolov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Scott
Top achievements
Rank 1
Answers by
Hristo Deshev
Telerik team
Scott
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or