How Popular a Listview, and her Popular another Listview by Primary Key?

Asked

Viewed 349 times

3

I have two Listviews that I need popular using LINQ to SQL (C#, WPF project) from a search field. When typing in the field(textbox) the Keydown event will popular the first Listview (1), as the second has a column that is the Primary Key of the first, I need that in this second Listview (2) search the populated Primary Key in the first Listview (1). It is possible?

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

Understand: Databases_1 Popula Listview_1 Databases_2 Popula 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

First Listview Search Method(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>

1 answer

2


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

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 placing the search for the second listview on Event onchange (or WPF equivalent) of the first list view.

  • Opa... Daniel as Listviews query the database by the LINK to SQL tool. This way is not correct the expression listView_1.Selecteditems[0]. Code .... Can arrange?

  • I removed the listview of the Linq call. Try now

  • Daniel... It didn’t work... Listview needs to be populated by the LINQ method.. I also didn’t find the Onchange function in the events... Project is in WPF.

  • But what mistake it makes?

  • I can only see today at 8:30 pm (Brasilia time)... Can you stay online this time? So we can exchange a better idea in the chat here... May?

  • So far it doesn’t run because Selecteditems[0]. Code is not recognized...

  • Daniel, it worked out partially... Because the idea is to type in the textbox the data of the first Database appear in the first Listview, and from this search the Code populated in the first Listview search similar data in the second Database to popular the Second Listview... This method can only search for a single code... What I want to get more than one searched and popular code these similar codes in the second Listview... You know?

  • For example... It may be that the user in the textbox searches for the word "Ma" in this case will be populated: Maca, Melao and Strawberry; Codes 202, 303 and 505. In this case you need to populate these codes in the Second Listview...

  • Coming soon... I check your update Daniel!

  • The Conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value... That was the error... It has to do with the fact that there is some column with Dates?

  • Show, Daniel!! Thank you so much!!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.