根据第一个组合框上的索引过滤第二个组合框

时间:2013-04-04 07:35:56

标签: sql vb.net winforms combobox

   Public Sub FiltercmbSubCategory()
    Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
    "Database = EOEMS;integrated security=true"

    Dim sqlcommand As SqlCommand

    sqlconn.Open()
    Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentSubCategory WHERE SUBCAT_ID = '" & cmbCategory.Text & "'", sqlconn)
    Dim dt As New DataTable
    da.Fill(dt)
    cmbCategory.DataSource = dt
    cmbCategory.DisplayMember = "SUBCAT_Name"
    cmbCategory.ValueMember = "SUBCAT_ID"
    sqlconn.Close()
End Sub

当我将此代码放在表单加载事件上时,第一个组合框上的数据消失了 当我把这个代码放在第一个组合框的index_changed上时

但是当我评论此代码时,它会再次在组合框1中显示记录

我需要根据CATEGORY_COMBOBOX过滤SUB_CATEGORY_COMBO_BOX

5 个答案:

答案 0 :(得分:1)

将你的第一个组合框绑在里面:

if (!Page.IsPostBack)
{
  //  Bind combobox1 code here;
}

现在,在selectedindexchange上调用绑定子类别组合的代码。

另外,请再次查看您的代码:

Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentSubCategory WHERE SUBCAT_ID = '" & cmbCategory.Text & "'", sqlconn)
Dim dt As New DataTable
da.Fill(dt)
cmbCategory.DataSource = dt
cmbCategory.DisplayMember = "SUBCAT_Name"
cmbCategory.ValueMember = "SUBCAT_ID"

在此代码中,您传递cmbCategory.Text作为参数,用于绑定相同的下拉列表cmbCategory。我想你错过了第二次下拉。可能是我不正确,但似乎就是这样。

答案 1 :(得分:0)

subcat_id是字符串还是整数?如果它的整数删除了where子句中的单引号。

另外对我而言,您似乎正在填充从数据中选择的相同组合框,而不是子框?

    cmbCategory[SUB?].DataSource = dt

答案 2 :(得分:0)

 Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox2.Items.Add(ComboBox1.SelectedIndex)
    End Sub

我想是这样的,你想要的是,你在1combobox上选择什么,它会显示到combobox2?是这样的?

<强>更新

     Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 

sqlconn.Open()
    Dim da1 As New SqlDataAdapter("SELECT Column_name FROM tblOfficeEquipmentSubCategory 
WHERE column_name_ = '" % & combobox1.selecteditem & % "'", sqlconn)
   From here >>> Dim dt1 As New DataTable
    da1.Fill(dt)
    cmbsubCategory.DataSource = dt1
    cmbsubCategory.DisplayMember = "SUBCAT_Name"
    cmbsubCategory.ValueMember = "SUBCAT_ID"
    sqlconn.Close()
                    End Sub <<< till here, you need to change yourself

答案 3 :(得分:0)

您已根据CATEGORY_COMBOBOX

为SUB_CATEGORY_COMBO_BOX使用了相同的对象cmbCategory

您应该将cmbSubCategory用于SUB_CATEGORY_COMBO_BOX,将cmbCategory用于CATEGORY_COMBOBOX

 Public Sub FiltercmbSubCategory()
            Dim sqlconn As New SqlClient.SqlConnection
            sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
            "Database = EOEMS;integrated security=true"

            Dim sqlcommand As SqlCommand

            sqlconn.Open()
            Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentSubCategory WHERE SUBCAT_ID = '" & cmbCategory.Text & "'", sqlconn)
            Dim dt As New DataTable
            da.Fill(dt)
            cmbSubCategory.DataSource = dt
            cmbSubCategory.DisplayMember = "SUBCAT_Name"
            cmbSubCategory.ValueMember = "SUBCAT_ID"
            cmbSubCategory.databind();
            sqlconn.Close()
        End Sub

希望这有帮助

答案 4 :(得分:0)

 Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
    "Database = EOEMS;integrated security=true"

    Dim dt As New DataTable

    sqlconn.Open()
    Dim da As New SqlDataAdapter("SELECT SUBCAT_ID FROM tblOfficeEquipmentSubCategory WHERE CAT_ID = '" & cmbCategory.Text & "'", sqlconn)
    da.Fill(dt)
    cmbSubCategory.DataSource = dt
    cmbSubCategory.DisplayMember = "SUBCAT_Name"
    cmbSubCategory.ValueMember = "SUBCAT_ID"
    sqlconn.Close()

得到了正确答案谢谢大家