【发布时间】:2021-12-27 17:07:20
【问题描述】:
我的 .xaml 中有一个组合框
<ComboBox Name="Group" Margin="50,71,330,618" Grid.Column="1"/>
以及我的 .xaml 中的另一个组合框
<ComboBox Name="Sort" Margin="20,71,0,618"/>
当我从我的组合框中选择一个项目(如“液体”)时,我只想查看表 Sort 中具有 GroupName Liquid 的项目。
我的 FK/PK 在数据库中是正确的,但我不知道如何将其放入我的 WPF。
我在 WPF 中使用 Visual Studio 2019,C#
在我的 .cs 文件中,我有以下查询,因此它将始终显示组合框中的项目
public AddStock()
{
InitializeComponent();
bindcomboboxgroup();
}
private void bindcomboboxgroup()
{
try
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"local";
string SelectQuery = "SELECT Name From [Group]";
con.Open();
SqlCommand cmd = new SqlCommand(SelectQuery, con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Groep.Items.Add(reader.GetString("Name"));
}
}
catch (Exception ex)
{
MessageBox.Show("Error occurred:\r\n" + ex.Message);
}
}
【问题讨论】:
标签: c# wpf data-binding datatable combobox