Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3334700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:56:03+00:00 2026-05-17T23:56:03+00:00

I have a form where user enters data about a new inventory item (id,title,

  • 0

I have a form where user enters data about a new inventory item (id,title, price, date, ect..). Data entered is saved to a collection and then redisplayed in a listview.

I get a “Specified cast is not valid” pointing to “key = (int)lvwInventory.SelectedItems[0];” when trying to extract the selected value. I’m trying to get the ID out and use it to open an edit window for that item.

What am I doing wrong here?

list view in question

<ListView Height="249" HorizontalAlignment="Left" Margin="27,28,0,0" Name="lvwInventory" VerticalAlignment="Top" Width="384" ItemsSource="{Binding col}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}" />
            <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" />
            <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" />
            <GridViewColumn Header="Date Added" DisplayMemberBinding="{Binding Date}" />
        </GridView>
    </ListView.View>
</ListView>

stockobject class

public class StockObject
    {
        // create stock object properties
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime Date { get; set; }
        public double Price { get; set; }
        public char Type { get; set; }

Event that’s trying to get value out of the selected item

private void btnViewItem_Click(object sender, RoutedEventArgs e)
    {
        int key;

        if (lvwInventory.SelectedIndex == -1) return;

        key = (int)lvwInventory.SelectedItems[0];

        StockObject s = col[key];
        bookDialog.View(s);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:56:03+00:00Added an answer on May 17, 2026 at 11:56 pm

    The selected item is the StockObject itself, which is why it won’t let you cast it to an int. You need something like this:

    var selectedStockObject = lvwInventory.SelectedItems[0] as StockObject;
    if(selectedStockObject == null)
    {
       return;
    }
    
    key = selectedStockObject.ID; //I'm assuming you want the ID as your key
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form on a webpage. When the user enters input into form,
I have a web form, that needs to save the data that the user
I have a form a user can enter their name, then it will add
In my current application I have a form that requires the user to enter
On a website if I have a form where the user can input some
I have a form which takes both the user details and an image uploaded
We have a form that allows a user to filter a list with by
I have a form in which the user can choose a component type from
I have a form on a page where the user has inputs to edit
I have to display a column chart in a user form in VBA. Exporting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.