如何使用外键将两个组合框链接起来
本文关键字:两个 组合 起来 链接 何使用 | 更新日期: 2023-09-27 18:21:42
我有两张表,一张是客户/公司的,另一张是该公司的代表。我在WCF中编写了一个方法,将两个表连接起来,以便它们一起工作。应该发生的是,如果我在一个组合框中选择了一个客户/公司,那么第二个组合框应该填充该公司的代表。
WCF
public List<Represetative> GetRepByComp(string CompanyId)
{
TruckDb db = new TruckDb();
List<Represetative> result = new List<Represetative>();
Represetative x = new Represetative();
Company o = new Company();
var filter = (from reps in db.Represetatives
join company in db.Companies on reps.Id equals company.Id
where company.Id == int.Parse(CompanyId)
select reps);
foreach (var item in filter)
{
result.Add(new Represetative()
{
Id = item.Id,
Name = item.Name
});
}
return result;
}
WPF
private async Task LoadClient(TruckServiceClient TSC, ComboBox combobox)
{
List<ClientItems> clientItems = new List<ClientItems>();
foreach (var item in await TSC.GetCompaniesAsync())
clientItems.Add(new ClientItems { Client = item.Name });
combobox.ItemsSource = (clientItems.ToArray());
combobox.IsEnabled = true;
combobox.SelectedIndex = 1;
}
public class ClientItems
{
public int Id { get; set; }
public string Client { get; set; }
public string RepId { get; set; }
public override string ToString()
{
return Client;
}
}
我的问题是为"代表"组合框创建一种方法,以便它与"客户/公司"组合框协同工作,有什么想法吗?
正如Chris所说,订阅SelectionChanged事件:
<ComboBox x:Name="comboBoxCompanies"
SelectionChanged="ComboBoxCompaniesSelectionChanged" />
以及后面的相应代码:
private void ComboBoxCompaniesSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Client client = e.AddedItems[0] as Client;
if (client != null)
{
TruckServiceClient tsc = null; // ...
List<Representative> representatives = GetRepByComp(tsc, client.Id.ToString());
comboBoxRepresentatives.ItemsSource = representatives;
}
}
问候