0

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 Answer 1

1

DataRowViewHave you tried just itterating through the SelectedItems?

    foreach (DataRowView row in Listview1.SelectedItems)
    {
         ...
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I have. So that allows me to access each row individually - within that row I have several columns and I am trying to pull data from those rows. i was iterating through the datarowview row itemarray... however, the position of the elements I need seem to be all over the place and I don't feel like the solution is very good. Hopefully that makes sense?
The order will be the order in the select statement used to create the datatable. I believe you can also use the column name e.g. row["ColX"]. There is even syntax to retrieve the column name based on the position.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.