0

I am populating a ListView from a Linq to SQL query list with this:

    private void loademployeelist()
    {
        dbml.hrdbDataContext listpeople = new dbml.hrdbDataContext();
        var q = from p in listpeople.EmpIDs
                join pi in listpeople.EmpBasicFiles on p.EmpID1 equals pi.EBF_EmpID
                where pi.EBF_Active == activeornot
                orderby p.LastName
                select new classes.Employeeselected() { FirstName = p.FirstName, LastName = p.LastName, empid=p.EmpID1};

        foreach (var item in q)
        {
            lb_active_employee_names.Items.Add(new { LastName = item.LastName, FirstName = item.FirstName, empid = item.empid });
        }
    }

The problem is that I just want the selected value to be the empid. I have looked everywhere and can't find a way to get that value. The code to get the value is this:

    private void lb_active_employee_names_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListViewItem empsel = (ListViewItem)lb_active_employee_names.SelectedItem;
        string test = empsel.GetType().ToString();
        MessageBox.Show(test);
      }

I keep getting an error when running the program after I select it. I tried looking up the error, but I am getting lost into this vast wilderness. I think I conceptually understand what is going on, but I don't know how to fix it. I am still very new to C#/Visual Studio.

Here is the error:

An exception of type 'System.InvalidCastException' occurred in 6DegreesGateway.exe but was not handled in user code

Additional information: Unable to cast object of type '<>f__AnonymousType103[System.String,System.String,System.Decimal]' to type 'System.Windows.Controls.ListViewItem'.

My XAML looks like this:

        <ListView x:Name="lb_active_employee_names" HorizontalAlignment="Left" Height="135" Margin="10,148,0,0" VerticalAlignment="Top" Width="365" Grid.Column="1" Grid.Row="1" Loaded="listBox_Loaded" SelectionChanged="lb_active_employee_names_SelectionChanged" SelectedItem="{Binding Path=empid}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
                <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" />
                <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding empid}" />
            </GridView>
        </ListView.View>
    </ListView>

I tried reviewing what others have done, Binding or creating a class etc. But I am just getting more and more confused.

James P
  • 2,201
  • 2
  • 20
  • 30
Jeff Ku
  • 15
  • 5
  • What do you mean by 'I just want the selected value to be the empid'? What are you trying to do with the 'empid'? – Jason Boyd May 31 '16 at 18:48
  • Take a look at this similar question. I think the answer will help. http://stackoverflow.com/questions/15091400/get-single-listview-selecteditem –  Jun 01 '16 at 02:30
  • @JasonBoyd I will be running another query from empid on another table. – Jeff Ku Jun 01 '16 at 19:40
  • @RudyTheHunter I did look at that site, but it did not work for me. I think I figured out my own solution. I will post it below. – Jeff Ku Jun 01 '16 at 19:46

1 Answers1

0

The code I used when adding the items to the listbox changed the type to anonymous.

foreach (var item in q)
    {
        lb_active_employee_names.Items.Add(new { LastName = item.LastName,     FirstName = item.FirstName, empid = item.empid });
    }

Instead of that I changed it to this:

        foreach (var item in q)
        {
            lb_active_employee_names.Items.Add(item);
        }

This way, all of the items in the list, including empid retained their type (at least this is what I think happened). In any case, I can get the data I need (empid) from that with this line of code:

        var empsel = (classes.Employeeselected)lb_active_employee_names.SelectedItem;
        empIdSelected = empsel.empid;

And then I can run further queries with that code on other tables.

It's not pretty and I am sure there is a better way to do this.

Jeff Ku
  • 15
  • 5