Getting DataGrid current item into an object WPF
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 |
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 '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
add a comment |
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 '18 at 3:56
kit
1,1063816
1,1063816
asked Nov 22 '18 at 3:20
Abdullah EtizazAbdullah Etizaz
134
134
What isexprev
? Show us the code regardingexprev
– 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
add a comment |
What isexprev
? Show us the code regardingexprev
– 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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
add a comment |
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
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
add a comment |
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 '18 at 12:50
mm8mm8
84.1k81931
84.1k81931
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
add a comment |
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
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.
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 '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