c# sql读取数据,两个combobox怎么数据绑定, 获取第一个的combobox1的值,又要在combobox用到该怎么写?
下面是我的代码privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){if(comboBox...
下面是我的代码
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > -1)
{
if (cn.State == System.Data.ConnectionState.Open)
{
cn.Close();
}
cn.Open();
string sql = "select c_name from Course where cct_ID=(select cct_id from CourseContentType where cct_NAME='@comboBox1')";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@comboBox1", comboBox1.Text);
SqlDataAdapter adapter = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
comboBox2.Text = "==请选择==";
// 填充数据到DataSet
adapter.Fill(ds);
comboBox2.DataSource = ds.Tables[0];
//给combobox绑定数据源
comboBox2.DisplayMember = "c_id";
comboBox2.ValueMember = "c_name";
cn.Close();
}
} 展开
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > -1)
{
if (cn.State == System.Data.ConnectionState.Open)
{
cn.Close();
}
cn.Open();
string sql = "select c_name from Course where cct_ID=(select cct_id from CourseContentType where cct_NAME='@comboBox1')";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@comboBox1", comboBox1.Text);
SqlDataAdapter adapter = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
comboBox2.Text = "==请选择==";
// 填充数据到DataSet
adapter.Fill(ds);
comboBox2.DataSource = ds.Tables[0];
//给combobox绑定数据源
comboBox2.DisplayMember = "c_id";
comboBox2.ValueMember = "c_name";
cn.Close();
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询