0
   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

4

5 回答 5

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作为参数传递以绑定相同的 dropdown cmbCategory。我认为您在这里错过了第二个下拉菜单。可能是我不正确,但似乎是这样。

于 2013-04-04T07:47:43.710 回答
0

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

同样对我来说,您似乎正在用数据填充您从中选择的相同组合框,而不是子框?

    cmbCategory[SUB?].DataSource = dt
于 2013-04-04T07:42:26.600 回答
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
于 2013-04-04T07:47:22.697 回答
0

您基于 CATEGORY_COMBOBOX 为 SUB_CATEGORY_COMBO_BOX 使用了相同的对象 cmbCategory

您应该对 SUB_CATEGORY_COMBO_BOX 使用 cmbSubCategory,对 CATEGORY_COMBOBOX 使用 cmbCategory

 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

希望这可以帮助

于 2013-04-04T07:55:34.207 回答
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()

得到正确答案谢谢大家

于 2013-04-04T08:48:31.490 回答