Getting data from Listview WPF

1.4k views Asked by At

I have 2 listviews that serve different purposes. Short question is that I need to find out how to pull specific columns from a WPF listview to add them to properties of an object.

Explanation of what i'm doing:

Listview 1: Bound to a database table. A user changes a combo box in order to filter the table that the listview is bound to. - I do not need help with this.

Listview 2: This listview is bound to an observable collection with 3 properties. - I do not need help with this.

User action: The user selects a subset of items from Listview 1 and clicks "add". I want to add specific columns of listview 1 to the properties of an "employee" object and then added to an observable collection so they can be displayed in Listview 2.

What I have completed: The databinding of listview 1 and listview 2 work perfectly. I have an employee class with 3 properties (agent id, name, office). I created an observable collection that I will be adding the employees to - IM FINE with this part.

What I need: I need to know how to find the specific data of listview 1 in order to assign the correct pieces to the corresponding properties of the objects in my observable collection.

My attempt is really an epic fail.. I will loop through all selected items to get the data from each, but for my try I only used the first selected item:

    Class windEmployee
    Private Agents As New ObservableCollection(Of Employee)

    Private sub AgentData()
        Dim x As DataRowView
        X = Listview1.SelectedItems(0)
        Agents.Add(New Employee With {.AgentID = x.Row.Item(9), .Name = x.Row.Item(6)     & " " & x.Row.Item(7), .Office = x.Row.Item(16)}
    end sub
    End Class
1

There are 1 answers

2
paparazzo On

DataRowViewHave you tried just itterating through the SelectedItems?

    foreach (DataRowView row in Listview1.SelectedItems)
    {
         ...
    }