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.










share|improve this question
























  • 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















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.










share|improve this question
























  • 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













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.










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 at 3:56









kit

1,1081516




1,1081516










asked Nov 22 at 3:20









Abdullah Etizaz

64




64












  • 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


















  • 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
















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












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





share|improve this answer





















    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',
    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








    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





    share|improve this answer

























      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





      share|improve this answer























        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





        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 at 12:50









        mm8

        80.2k81831




        80.2k81831






























            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.





            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.




            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

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks