C# Winform- Combobox2 Changes when combobox1 change using Foreign key Sql 服务器

C# Winform- Combobox2 Changes when combobox1 change using Foreign key Sql server

我的 Winforms 应用程序中有 2 个组合框,一个用于 Table 1 个

Curricuculum table

CurriculumID (PK, auto-increment)
CurriculumName Varchar(255)

GradeLevel table

GradeLevelID (PK, auto-increment)
GradeLevel Varchar(255)
CurriculumID (foreign key)

数据示例:

课程table

CurriculumID   CurriculumName
-------------- ----------
1              Grade Levels
2              Kinder Levels
3              College Levels

年级table

GradeLevelID   CurriculumID   GradeLevelName
 -------       -------------- ----------
 1             1              Grade 1
 2             1              Grade 2
 3             1              Grade 3
 4             2              Kinder 1
 5             2              Kinder 2
 6             3              College 1
 7             3              College 2
 8             3              College 3

我想在 Combobox 1 中设置 Select 等级,然后 Grade 1、Grade 2、Grade 3 将显示在 Combobox 2 中。

问题: 到目前为止我使用的代码每当我 select Combobox1

中的任何内容时,都会显示年级水平(Combobox 2)中的所有列

到目前为止,这是我的代码

public schedulingForm()
        {
            InitializeComponent();
            CCombobox();
            GLCombobox();
        }



private void CCombobox()
        {
            string connetionString = null;
            SqlConnection connection;
            SqlCommand command;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            string sql = null;
            connetionString = "Data Source=.\KENNETH;Initial Catalog=HSPAEnrollmentSytem;Integrated Security=True";
            sql = "select CurriculumID,CurriculumName from Curriculum";
            connection = new SqlConnection(connetionString);
            try
            {
                connection.Open();
                command = new SqlCommand(sql, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds);
                adapter.Dispose();
                command.Dispose();
                connection.Close();
                this.cbCurriculum.DataSource = ds.Tables[0];
                this.cbCurriculum.ValueMember = "CurriculumID";
                this.cbCurriculum.DisplayMember = "CurriculumName";
                this.cbCurriculum.BindingContext = new BindingContext();
                this.comboBox1.DataSource = ds.Tables[0];
                this.comboBox1.ValueMember = "CurriculumID";
                this.comboBox1.DisplayMember = "CurriculumID";
                this.comboBox1.BindingContext = new BindingContext();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection!");
            }
        }






private void GLCombobox()
    {
        string connetionString = null;
        SqlConnection connection;
        SqlCommand command;
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        int i = 0;
        string sql = null;
        connetionString = "Data Source=.\KENNETH;Initial Catalog=HSPAEnrollmentSytem;Integrated Security=True";
        sql = "select GradeLevelID,GradeLevel from GradeLevel";
        connection = new SqlConnection(connetionString);
        try
        {
            connection.Open();
            command = new SqlCommand(sql, connection);
            adapter.SelectCommand = command;
            adapter.Fill(ds);
            adapter.Dispose();
            command.Dispose();
            connection.Close();
            this.cbGradeLevel.DataSource = ds.Tables[0];
            this.cbGradeLevel.ValueMember = "GradeLevelID";
            this.cbGradeLevel.DisplayMember = "GradeLevel";
            this.cbGradeLevel.BindingContext = new BindingContext();
            this.comboBox2.DataSource = ds.Tables[0];
            this.comboBox2.ValueMember = "GradeLevelID";
            this.comboBox2.DisplayMember = "GradeLevelID";
            this.comboBox2.BindingContext = new BindingContext();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection!");
        }
    }

private void cbCurriculum_SelectedIndexChanged(object sender, EventArgs e)
    {

        GLCombobox();

    }

在此处添加过滤器

sql = "select GradeLevelID,GradeLevel from GradeLevel where CurriculumID = '" +selected_value_cb1 +"'";