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>
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?
– Felipe Cavalcante
I removed the listview of the Linq call. Try now
– Daniel Santos
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.
– Felipe Cavalcante
But what mistake it makes?
– Daniel Santos
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?
– Felipe Cavalcante
So far it doesn’t run because Selecteditems[0]. Code is not recognized...
– Felipe Cavalcante
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?
– Felipe Cavalcante
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...
– Felipe Cavalcante
Coming soon... I check your update Daniel!
– Felipe Cavalcante
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?
– Felipe Cavalcante
Show, Daniel!! Thank you so much!!
– Felipe Cavalcante