Getting DataGrid current item into an object WPF
up vote
-1
down vote
favorite
I am having trouble casting WPF DataGrid's current item to a linq-generated
entity since a few days. It would be amazing if someone can help me out of this one.
I am getting data for the DataGrid using linq query as below:
Dim payableexpense As New exprev
Dim listofexpenses As IEnumerable(Of exprev)
Private Sub txtsearchname_TextChanged(sender As Object, e As TextChangedEventArgs) Handles txtsearchname.TextChanged
listofexpenses = From expenses In db.exprevs
Where expenses.school_id = currentschool.school_id AndAlso expenses.title.Contains(txtsearchname.Text.ToString) AndAlso expenses.status <> 1 AndAlso expenses.type = 1
dgvsearchresults.ItemsSource = listofexpenses
dgvsearchresults.Items.Refresh()
stpexpensedetails.DataContext = payableexpense
End Sub
And for the DataGrid selection changed event, I have this code:
Private Sub dgvsearchresults_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dgvsearchresults.SelectionChanged
payableexpense = dgvsearchresults.CurrentItem
End Sub
But during run-time
, it is throwing an exception that:
"unable to cast object of type 'ms.internal.namedobject' to 'exprev'"
At another place in my project, the same approach is working fine.
.net wpf vb.net linq linq-to-sql
add a comment |
up vote
-1
down vote
favorite
I am having trouble casting WPF DataGrid's current item to a linq-generated
entity since a few days. It would be amazing if someone can help me out of this one.
I am getting data for the DataGrid using linq query as below:
Dim payableexpense As New exprev
Dim listofexpenses As IEnumerable(Of exprev)
Private Sub txtsearchname_TextChanged(sender As Object, e As TextChangedEventArgs) Handles txtsearchname.TextChanged
listofexpenses = From expenses In db.exprevs
Where expenses.school_id = currentschool.school_id AndAlso expenses.title.Contains(txtsearchname.Text.ToString) AndAlso expenses.status <> 1 AndAlso expenses.type = 1
dgvsearchresults.ItemsSource = listofexpenses
dgvsearchresults.Items.Refresh()
stpexpensedetails.DataContext = payableexpense
End Sub
And for the DataGrid selection changed event, I have this code:
Private Sub dgvsearchresults_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dgvsearchresults.SelectionChanged
payableexpense = dgvsearchresults.CurrentItem
End Sub
But during run-time
, it is throwing an exception that:
"unable to cast object of type 'ms.internal.namedobject' to 'exprev'"
At another place in my project, the same approach is working fine.
.net wpf vb.net linq linq-to-sql
What isexprev
? Show us the code regardingexprev
– zack raiyan
Nov 22 at 4:21
@zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.
– Abdullah Etizaz
Nov 22 at 12:00
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am having trouble casting WPF DataGrid's current item to a linq-generated
entity since a few days. It would be amazing if someone can help me out of this one.
I am getting data for the DataGrid using linq query as below:
Dim payableexpense As New exprev
Dim listofexpenses As IEnumerable(Of exprev)
Private Sub txtsearchname_TextChanged(sender As Object, e As TextChangedEventArgs) Handles txtsearchname.TextChanged
listofexpenses = From expenses In db.exprevs
Where expenses.school_id = currentschool.school_id AndAlso expenses.title.Contains(txtsearchname.Text.ToString) AndAlso expenses.status <> 1 AndAlso expenses.type = 1
dgvsearchresults.ItemsSource = listofexpenses
dgvsearchresults.Items.Refresh()
stpexpensedetails.DataContext = payableexpense
End Sub
And for the DataGrid selection changed event, I have this code:
Private Sub dgvsearchresults_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dgvsearchresults.SelectionChanged
payableexpense = dgvsearchresults.CurrentItem
End Sub
But during run-time
, it is throwing an exception that:
"unable to cast object of type 'ms.internal.namedobject' to 'exprev'"
At another place in my project, the same approach is working fine.
.net wpf vb.net linq linq-to-sql
I am having trouble casting WPF DataGrid's current item to a linq-generated
entity since a few days. It would be amazing if someone can help me out of this one.
I am getting data for the DataGrid using linq query as below:
Dim payableexpense As New exprev
Dim listofexpenses As IEnumerable(Of exprev)
Private Sub txtsearchname_TextChanged(sender As Object, e As TextChangedEventArgs) Handles txtsearchname.TextChanged
listofexpenses = From expenses In db.exprevs
Where expenses.school_id = currentschool.school_id AndAlso expenses.title.Contains(txtsearchname.Text.ToString) AndAlso expenses.status <> 1 AndAlso expenses.type = 1
dgvsearchresults.ItemsSource = listofexpenses
dgvsearchresults.Items.Refresh()
stpexpensedetails.DataContext = payableexpense
End Sub
And for the DataGrid selection changed event, I have this code:
Private Sub dgvsearchresults_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dgvsearchresults.SelectionChanged
payableexpense = dgvsearchresults.CurrentItem
End Sub
But during run-time
, it is throwing an exception that:
"unable to cast object of type 'ms.internal.namedobject' to 'exprev'"
At another place in my project, the same approach is working fine.
.net wpf vb.net linq linq-to-sql
.net wpf vb.net linq linq-to-sql
edited Nov 22 at 3:56
kit
1,1081516
1,1081516
asked Nov 22 at 3:20
Abdullah Etizaz
64
64
What isexprev
? Show us the code regardingexprev
– zack raiyan
Nov 22 at 4:21
@zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.
– Abdullah Etizaz
Nov 22 at 12:00
add a comment |
What isexprev
? Show us the code regardingexprev
– zack raiyan
Nov 22 at 4:21
@zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.
– Abdullah Etizaz
Nov 22 at 12:00
What is
exprev
? Show us the code regarding exprev
– zack raiyan
Nov 22 at 4:21
What is
exprev
? Show us the code regarding exprev
– zack raiyan
Nov 22 at 4:21
@zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.
– Abdullah Etizaz
Nov 22 at 12:00
@zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.
– Abdullah Etizaz
Nov 22 at 12:00
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Use the TryCast
method to try to cast the SelectedItem
property to an exprev
:
Private Sub dgvsearchresults_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dgvsearchresults.SelectionChanged
Dim selectedItem As exprev = TryCast(dgvsearchresults.SelectedItem, exprev)
If selectedItem IsNot Nothing Then
payableexpense = selectedItem
'...
End If
End Sub
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Use the TryCast
method to try to cast the SelectedItem
property to an exprev
:
Private Sub dgvsearchresults_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dgvsearchresults.SelectionChanged
Dim selectedItem As exprev = TryCast(dgvsearchresults.SelectedItem, exprev)
If selectedItem IsNot Nothing Then
payableexpense = selectedItem
'...
End If
End Sub
add a comment |
up vote
0
down vote
Use the TryCast
method to try to cast the SelectedItem
property to an exprev
:
Private Sub dgvsearchresults_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dgvsearchresults.SelectionChanged
Dim selectedItem As exprev = TryCast(dgvsearchresults.SelectedItem, exprev)
If selectedItem IsNot Nothing Then
payableexpense = selectedItem
'...
End If
End Sub
add a comment |
up vote
0
down vote
up vote
0
down vote
Use the TryCast
method to try to cast the SelectedItem
property to an exprev
:
Private Sub dgvsearchresults_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dgvsearchresults.SelectionChanged
Dim selectedItem As exprev = TryCast(dgvsearchresults.SelectedItem, exprev)
If selectedItem IsNot Nothing Then
payableexpense = selectedItem
'...
End If
End Sub
Use the TryCast
method to try to cast the SelectedItem
property to an exprev
:
Private Sub dgvsearchresults_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dgvsearchresults.SelectionChanged
Dim selectedItem As exprev = TryCast(dgvsearchresults.SelectedItem, exprev)
If selectedItem IsNot Nothing Then
payableexpense = selectedItem
'...
End If
End Sub
answered Nov 22 at 12:50
mm8
80.2k81831
80.2k81831
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423398%2fgetting-datagrid-current-item-into-an-object-wpf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is
exprev
? Show us the code regardingexprev
– zack raiyan
Nov 22 at 4:21
@zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.
– Abdullah Etizaz
Nov 22 at 12:00