【问题标题】:Select Combobox items when selecting a group选择组时选择组合框项目
【发布时间】: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


    【解决方案1】:

    以下解决方案似乎对你来说已经足够了:

    我创建了两个表(组表和排序表),其值如下:

    注意:如果需要使用 Application.StartupPath,请添加 System.Windows.Forms 引用。

    using System.Windows;
    using System.Windows.Controls;
    namespace WpfApp
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            System.Data.SqlClient.SqlConnection SQLConnect = new System.Data.SqlClient.SqlConnection("Data Source = .\\;Integrated Security=True;AttachDbFilename=" + System.Windows.Forms.Application.StartupPath + @"\LocalDB.mdf;");
            public MainWindow()
            {
                InitializeComponent();
                System.Data.SqlClient.SqlCommand CMD = new System.Data.SqlClient.SqlCommand("Select * from GroupTable", SQLConnect);
                SQLConnect.Open();
                System.Data.SqlClient.SqlDataReader SDR = CMD.ExecuteReader();
                GroupComboBox.Items.Clear();
                while (SDR.Read())
                {
                    for (int i = 0; i < SDR.FieldCount; i++)
                    {
                        GroupComboBox.Items.Add(SDR[i].ToString());
                    }
                }
                SQLConnect.Close();
            }
    
            private void Group_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                System.Data.SqlClient.SqlCommand CMD;
                switch (GroupComboBox.SelectedItem.ToString())
                {
                    case "Gas":
                        CMD = new System.Data.SqlClient.SqlCommand("Select G from SortTable", SQLConnect);
                        break;
                    case "Liquid":
                        CMD = new System.Data.SqlClient.SqlCommand("Select L from SortTable", SQLConnect);
                        break;
                    case "Plasma":
                        CMD = new System.Data.SqlClient.SqlCommand("Select P from SortTable", SQLConnect);
                        break;
                    default:
                        CMD = new System.Data.SqlClient.SqlCommand("Select S from SortTable", SQLConnect);
                        break;
                }
                SQLConnect.Open();
                System.Data.SqlClient.SqlDataReader SDR = CMD.ExecuteReader();
                SortComboBox.Items.Clear();
                while(SDR.Read())
                {
                    SortComboBox.Items.Add(SDR[0].ToString());    
                }
                SQLConnect.Close();
            }
        }
    }
    

    输出:

    测试于:

    Visual Studio 2017、.NET Framework 4.5.2、WPF、SQL Server 12.0.6024.0

    谢谢

    【讨论】:

    • 注意:如果这个解决方案对你有帮助,请标记为答案(别忘了给我投票)
    • 我会的!我无法更早地对其进行测试。我现在正在努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多