How to Popular a Listview, and Popular Another Listview by Primary Key?

3

I have two ListViews that I need to use LINQ to SQL (C #, WPF project) in a search field. When typing in the textbox field the KeyDown event will populate the first ListView (1), as the second has a column that is the Primary Key of the first, I need in that second ListView (2) to look for the Primary Key populated in the first ListView 1). It's possible?

Detail: In both lists variables are strings but have different values. For example, in ListiView (1) you search for a Description and in the ListView (2) a Primary Key that are numbers (although it is a string) that is in the ListView (1) as well.

Understand: DataBases_1 Population ListView_1          DataBases_2 Populate ListView_2

DataBases_1
Codigo(string_PK) | Descricao (string)
101               | Uva 
202               | Maca
303               | Melao
404               | Salada Mista
505               | Morango

DataBases_2
Nome              | Codigo(string_PK) 
Joao              | 404
Maria             | 404
Paulo             | 505
Felipe            | 101
Renata            | 202

Search Method of First ListView (1) - KeyDown Event:

using (DataClassesDataContext oDB = DataClassesDataContext())
{
    var busca = from p in oDB.DataBases where p.Descricao.Contains(txtPesquisa.Text) orderby p.Codigo descending select p;
    listView_1.ItemsSource = busca.ToList();
    listView_2.ItemsSource = ??????? ---> //Popular com base na lista encontrada pelo ListView_1 (var busca)
}

XMAL:

       <ListView x:Name="listView_1" Margin="34,345,618,51" ItemsSource="{Binding}" ScrollViewer.CanContentScroll="True" SelectionMode="Extended"><ListView.View>                                                                  <GridView>
    <GridViewColumn Header="Código" Width="Auto"  DisplayMemberBinding="{Binding Codigo/>
    <GridViewColumn Header="Descricao" Width="Auto" DisplayMemberBinding="{Binding Descricao}"/>
            </GridView>
            </ListView.View>
            </ListView>

        <ListView x:Name="listView_2" Margin="1050,345,72,51" ItemsSource="{Binding}" ScrollViewer.CanContentScroll="True" SelectionMode="Extended">
        <ListView.View>     

        <GridView>
<GridViewColumn Header="Nome" Width="Auto"  DisplayMemberBinding="{Binding Nome}"/>
<GridViewColumn Header="Código" Width="Auto" DisplayMemberBinding="{Binding Codigo}"/>
            </GridView>
            </ListView.View>
            </ListView>
    
asked by anonymous 25.06.2016 / 22:14

1 answer

2

You can do a second search using the codes of the first search:

using (DataClassesDataContext oDB = DataClassesDataContext())
{
    var busca = from p in oDB.DataBases_1 where p.Descricao.Contains(txtPesquisa.Text) orderby p.Codigo descending select p;
    listView_1.ItemsSource = busca.ToList();
    var codigosList1 = busca.Select((b) => b.Codigo).ToList();
    var busca2 = from p2 in oDB.DataBases_2 where codigosList1.Contains(p2.Codigo) orderby p2.Codigo descending select p2;
    listView_2.ItemsSource = busca2.ToList();
}

I recommend putting the second listview search in the event list (or WPF equivalent) of the first list view.

    
04.07.2016 / 17:04