使用 2 个组合框填充 VB.NET 中的数据网格视图

Using 2 combo boxes to populate a datagridview in VB.NET

我无法理解为什么我的一个组合框显示过滤搜索而另一个不显示,我对两个组合框使用相同的代码但修改了链接到我的数据库的一些 SQL 查询。我还注意到,当我删除或注释掉任何一个组合框的代码时,过滤搜索会针对未被注释或删除的组合框进行。我还使用了“If, Else”语句,但仍然不起作用。我还希望两个组合框都用于过滤数据网格视图。请记住,一旦从组合框中选择了项目,就会按下搜索按钮 filer/display 数据到数据网格视图中。

亲切的问候

这是我的代码和表格:

[正在显示冗余数据]https://i.stack.imgur.com/JEQI4.png

[ComboBox 品牌按预期工作]https://i.stack.imgur.com/6YyBf.png

[ComboBox 类别显示所有内容而不是显示所选类别]https://i.stack.imgur.com/oEfII.png

Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click

     If Not CmbBrand.SelectedIndex & CmbCategory.SelectedIndex = Nothing Then
         BrandDisplay()
     ElseIf CmbBrand.SelectedIndex & Not CmbCategory.SelectedIndex = Nothing Then
         CategoryDisplay()
     ElseIf Not CmbBrand.SelectedIndex & Not CmbCategory.SelectedIndex = Nothing Then

If DbConnect() Then 
             DgvRecord.Rows.Clear()
             Dim SQLCmd As New OleDbCommand 
             With SQLCmd 
                 .Connection = cn 
                 .CommandText = "Select * " &
                                "From TblStock " &
                                "Where STCategory Like @CategorySearch" 
                 .Parameters.AddWithValue("@CategorySearch", "%" & CmbCategory.Text & "%") 
                 Dim rs As OleDbDataReader = .ExecuteReader() 
                 SQLCmd.ExecuteReader()
                 While rs.Read 
                     Dim NewStockRow As New DataGridViewRow()
                     NewStockRow.CreateCells(DgvRecord)
                     NewStockRow.SetValues({rs("StockID"), rs("STDateTime"), rs("STCategory"), rs("STBrand"), rs("STItemDescription"), rs("STSerialNumber"), rs("StockIn"), rs("StockOut"), rs("Stock")})
                     NewStockRow.Tag = rs("StockID")
                     DgvRecord.Rows.Add(NewStockRow)
                 End While
                 rs.Close()

                 If DgvRecord.Rows(0).Selected = True Then
                     MessageBox.Show("Please select a Category from the drop down list", "Category", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                 End If
             End With
         End If
     End If
     cn.Close()
 End Sub

 Private Sub BrandDisplay()
     If DbConnect() Then 
         DgvRecord.Rows.Clear()
         Dim SQLCmd As New OleDbCommand 
         With SQLCmd 
             .Connection = cn 
             .CommandText = "Select * " &
                            "From TblStock " &
                            "Where STBrand Like @BrandSearch" 
             .Parameters.AddWithValue("@BrandSearch", "%" & CmbBrand.Text & "%") 
             Dim rs As OleDbDataReader = .ExecuteReader() 
             SQLCmd.ExecuteReader()
             While rs.Read 
                 Dim NewStockRow As New DataGridViewRow()
                 NewStockRow.CreateCells(DgvRecord)
                 NewStockRow.SetValues({rs("StockID"), rs("STDateTime"), rs("STCategory"), rs("STBrand"), rs("STItemDescription"), rs("STSerialNumber"), rs("StockIn"), rs("StockOut"), rs("Stock")})
                 NewStockRow.Tag = rs("StockID")
                 DgvRecord.Rows.Add(NewStockRow)
             End While
             rs.Close() 

             If DgvRecord.Rows(0).Selected = True Then
                 MessageBox.Show("Please select a Brand from the drop down list", "Brand", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
             End If
         End With
     End If
     cn.Close()
 End Sub

 Private Sub CategoryDisplay()
     If DbConnect() Then 
         DgvRecord.Rows.Clear()
         Dim SQLCmd As New OleDbCommand 
         With SQLCmd 
             .Connection = cn 
             .CommandText = "Select * " &
                            "From TblStock " &
                            "Where STCategory Like @CategorySearch" 
             .Parameters.AddWithValue("@CategorySearch", "%" & CmbCategory.Text & "%")
             Dim rs As OleDbDataReader = .ExecuteReader() 
             SQLCmd.ExecuteReader()
             While rs.Read 
                 Dim NewStockRow As New DataGridViewRow()
                 NewStockRow.CreateCells(DgvRecord)
                 NewStockRow.SetValues({rs("StockID"), rs("STDateTime"), rs("STCategory"), rs("STBrand"), rs("STItemDescription"), rs("STSerialNumber"), rs("StockIn"), rs("StockOut"), rs("Stock")})
                 NewStockRow.Tag = rs("StockID")
                 DgvRecord.Rows.Add(NewStockRow)
             End While
             rs.Close() 

             If DgvRecord.Rows(0).Selected = True Then
                 MessageBox.Show("Please select a Category from the drop down list", "Category", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
             End If
         End With
     End If
     cn.Close()
 End Sub
 ```

为了更好地理解,您需要首先更改那些“如果...那么...否则...”。 如果未选择组合框,它将具有值 -1,因此您可以这样做:

Dim bBrandIsSelected as boolean = CmbBrand.SelectedIndex <> -1
Dim bCategoryIsSelected as boolean = CmbCategory.SelectedIndex <> -1

现在您可以更轻松地构建代码,例如:

If bBrandIsSelected AndAlso bCategoryIsSelected then
  ' do something
else
    if bBrandIsSelected then  
        BrandDisplay()
    else
       if bCategoryIsSelected then
           CategoryDisplay()
       End if
    End if
 End if

最好将用户界面代码与数据库代码分开。您的事件过程代码应该相当简短。

在使用它们的方法中声明您的连接、命令和数据读取器,以便它们可以被释放。 Using...End Using 块为我们做这件事;他们也关闭了连接。将您的连接字符串直接传递给连接的构造函数。

对于每种可能性,我们都有不同的 CommandText 和 ParametersCollection。对于 Sql 服务器,使用 Add 方法而不是 AddWithValue。

Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
    Dim dt = GetSearchData(CmbBrand.Text, CmbCategory.Text)
    DGVRecord.DataSource = dt
End Sub

Private Function GetSearchData(Brand As String, Category As String) As DataTable
    Dim dt As New DataTable
    Dim sqlString = "Select * From From TblStock "
    Using cn As New SqlConnection("Your connection string"),
            cmd As New SqlCommand()
        cmd.Connection = cn
        If Not String.IsNullOrEmpty(Brand) AndAlso Not String.IsNullOrEmpty(Category) Then
            cmd.CommandText = sqlString & "Where STCategory = @CategorySearch And STBrand = @BrandSearch;"
            cmd.Parameters.Add("@CategorySearch", SqlDbType.VarChar).Value = Brand
            cmd.Parameters.Add("@BrandSearch", SqlDbType.VarChar).Value = Category
        ElseIf Not String.IsNullOrEmpty(Brand) Then
            cmd.CommandText = sqlString & "Where STBrand = @BrandSearch;"
            cmd.Parameters.Add("@BrandSearch", SqlDbType.VarChar).Value = Category
        ElseIf Not String.IsNullOrEmpty(Category) Then
            cmd.CommandText = sqlString & "Where STCategory = @CategorySearch;"
            cmd.Parameters.Add("@CategorySearch", SqlDbType.VarChar).Value = Brand
        Else
            cmd.CommandText = sqlString & ";"
        End If
        cn.Open()
        Using reader = cmd.ExecuteReader()
            dt.Load(reader)
        End Using
    End Using
    Return dt
End Function