我的Winforms应用程序中有2个组合框,一个用于表1一个
Curricuculum
表
CurriculumID (PK, auto-increment)
CurriculumName Varchar(255)
GradeLevel
表
GradeLevelID (PK, auto-increment)
GradeLevel Varchar(255)
CurriculumID (foreign key)
数据示例:
课程表
CurriculumID CurriculumName
-------------- ----------
1 Grade Levels
2 Kinder Levels
3 College Levels
GradeLevel表
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中选择成绩等级,然后在Combobox 2中出现1年级,2年级,3年级。
问题: 我到目前为止使用的代码每当我从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();
}
答案 0 :(得分:0)
在此处添加过滤器
sql = "select GradeLevelID,GradeLevel from GradeLevel where CurriculumID = '" +selected_value_cb1 +"'";