使用c#在windows窗体中级联comboBox

我试图在Windows窗体应用程序中从同一个表更改的combobox1 selectedText上填充Combobox2。 我正在使用sql serevr 2008数据库。 我无法在combobox上填充combobox2所选文本已更改。

这是我尝试过的:

private void Purchase_Load(object sender, EventArgs e) { fillName(); comboBoxName.SelectedIndex = -1; } private void comboBoxName_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxName.SelectedText != "") { fillMake(); } } private void fillName() { SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"); con.Open(); string str = "Select Item_Name from Item"; SqlCommand cmd = new SqlCommand(str, con); SqlDataAdapter adp = new SqlDataAdapter(str, con); DataTable dtItem = new DataTable(); adp.Fill(dtItem); cmd.ExecuteNonQuery(); comboBoxName.DataSource = dtItem; comboBoxName.DisplayMember = "Item_Name"; comboBoxName.ValueMember = "Item_Make"; } private void fillMake() { SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"); con.Open(); string str = "Select Item_Make from Item Where Item_Name='" + comboBoxName.SelectedText + "'"; SqlCommand cmd = new SqlCommand(str, con); SqlDataAdapter adp = new SqlDataAdapter(str, con); DataTable dtItem = new DataTable(); adp.Fill(dtItem); cmd.ExecuteNonQuery(); comboBoxName.DataSource = dtItem; comboBoxName.DisplayMember = "Item_Make"; comboBoxName.ValueMember = "Item_Name"; comboBoxName.SelectedIndex = -1; comboBoxName.Text = "Select"; } 

用于Items的Sql server表

 Item_Code Item_Name Item_Make Item_Price UnitofMeasurement Cable anchor 45.0000 meter Cable polycab 30.0000 meter Button anchor 15.0000 unit Button havells 20.0000 unit Switch cona 70.0000 unit 

我已经寻找解决方案,但很不幸。 请帮帮我。 提前致谢。

要弄清楚你想要做什么有点困难,但听起来你正在尝试填充第二个combobox( comboBoxMake ?),具体取决于在comboBoxName选择的内容。 我基于这个假设得出这个答案。 如果我有这个错误,请道歉。

这段代码中有很多东西需要注意。 我们先来看看fillName()

  private void fillName() { SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"); con.Open(); string str = "Select Item_Name from Item"; SqlCommand cmd = new SqlCommand(str, con); SqlDataAdapter adp = new SqlDataAdapter(str, con); DataTable dtItem = new DataTable(); adp.Fill(dtItem); cmd.ExecuteNonQuery(); comboBoxName.DataSource = dtItem; comboBoxName.DisplayMember = "Item_Name"; comboBoxName.ValueMember = "Item_Make"; } 

您需要Dispose()您的数据库对象。 using { .. }块可以非常干净地完成此操作。

您无需手动打开连接; 使用数据适配器填充表会自动执行此操作。

您不需要调用ExecuteNonQuery()

您应该使用带有命令对象的SqlDataAdapter构造函数重载,因为您已经手动创建了该命令。

最后,基于我对你的目标的假设,我为你的查询添加了一个distinct ,所以它只获得唯一的Item_Name

 private void fillName() { string str = "Select distinct Item_Name from Item"; using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True")) { using (SqlCommand cmd = new SqlCommand(str, con)) { using (SqlDataAdapter adp = new SqlDataAdapter(cmd)) { DataTable dtItem = new DataTable(); adp.Fill(dtItem); comboBoxName.DataSource = dtItem; comboBoxName.DisplayMember = "Item_Name"; comboBoxName.ValueMember = "Item_Name"; } } } } 

fillMake() 。 我在上面提到过同样的建议。 另外:

参数化您的SQL。 参数化您的SQL 。 这不仅比将SQL连接在一起更安全,而且更加清晰。 说真的,请阅读SQL注入: http : //en.wikipedia.org/wiki/SQL_injection

原始post中的fillMake()方法似乎重新填充了comboBoxName 。 它是否正确? 你提到了两个combobox,但你的代码只引用了一个。 我假设你的意思是在这里填充另一个combobox( comboBoxMake ?):

 private void fillMake() { string str = "Select Item_Make from Item Where Item_Name = @item_name"; using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True")) { using (SqlCommand cmd = new SqlCommand(str, con)) { cmd.Parameters.AddWithValue("@item_name", comboBoxName.Text); using (SqlDataAdapter adp = new SqlDataAdapter(cmd)) { DataTable dtItem = new DataTable(); adp.Fill(dtItem); comboBoxMake.DataSource = dtItem; comboBoxMake.DisplayMember = "Item_Make"; comboBoxMake.ValueMember = "Item_Make"; comboBoxMake.SelectedIndex = -1; comboBoxMake.Text = "Select"; } } } } 

最后,更改事件处理程序中的代码,使其查看Text而不是SelectedText属性:

 private void comboBoxName_SelectedIndexChanged(object sender, EventArgs e) { if (!string.IsNullOrEmpty(comboBoxName.Text)) // Text instead of SelectedText { fillMake(); } }