Getting DataGrid current item into an object WPF












0















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.










share|improve this question

























  • What is exprev ? Show us the code regarding exprev

    – zack raiyan
    Nov 22 '18 at 4:21











  • @zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.

    – Abdullah Etizaz
    Nov 22 '18 at 12:00
















0















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.










share|improve this question

























  • What is exprev ? Show us the code regarding exprev

    – zack raiyan
    Nov 22 '18 at 4:21











  • @zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.

    – Abdullah Etizaz
    Nov 22 '18 at 12:00














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 3:56









kit

1,1063716




1,1063716










asked Nov 22 '18 at 3:20









Abdullah EtizazAbdullah Etizaz

134




134













  • What is exprev ? Show us the code regarding exprev

    – zack raiyan
    Nov 22 '18 at 4:21











  • @zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.

    – Abdullah Etizaz
    Nov 22 '18 at 12:00



















  • What is exprev ? Show us the code regarding exprev

    – zack raiyan
    Nov 22 '18 at 4:21











  • @zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.

    – Abdullah Etizaz
    Nov 22 '18 at 12:00

















What is exprev ? Show us the code regarding exprev

– zack raiyan
Nov 22 '18 at 4:21





What is exprev ? Show us the code regarding exprev

– zack raiyan
Nov 22 '18 at 4:21













@zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.

– Abdullah Etizaz
Nov 22 '18 at 12:00





@zackraiyan exprev is a Linq-to-SQL generated entity based on a table in Database.

– Abdullah Etizaz
Nov 22 '18 at 12:00












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer
























  • Thank You this is exactly what I was looking for. Sorry for not marking it as and answer earlier.

    – Abdullah Etizaz
    Dec 9 '18 at 14:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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





share|improve this answer
























  • Thank You this is exactly what I was looking for. Sorry for not marking it as and answer earlier.

    – Abdullah Etizaz
    Dec 9 '18 at 14:00
















0














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





share|improve this answer
























  • Thank You this is exactly what I was looking for. Sorry for not marking it as and answer earlier.

    – Abdullah Etizaz
    Dec 9 '18 at 14:00














0












0








0







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 12:50









mm8mm8

82.2k81831




82.2k81831













  • Thank You this is exactly what I was looking for. Sorry for not marking it as and answer earlier.

    – Abdullah Etizaz
    Dec 9 '18 at 14:00



















  • Thank You this is exactly what I was looking for. Sorry for not marking it as and answer earlier.

    – Abdullah Etizaz
    Dec 9 '18 at 14:00

















Thank You this is exactly what I was looking for. Sorry for not marking it as and answer earlier.

– Abdullah Etizaz
Dec 9 '18 at 14:00





Thank You this is exactly what I was looking for. Sorry for not marking it as and answer earlier.

– Abdullah Etizaz
Dec 9 '18 at 14:00


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'