-1

我有一个带有数据源的数据网格视图和一个访问表。datagridview 已填充且完美,但现在我想创建一个带有过滤器可能性的额外组合框。

这是在我的页面加载功能中:

Private Sub ArchiefFacturatie_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'CharelIjsDataSet.tblFactuur' table. You can move, or remove it, as needed.

    Me.TblFactuurTableAdapter.Fill(Me.CharelIjsDataSet.tblFactuur)

'Filling comboxbox part skipped

End Sub

此时组合框也填充了datagridview。现在我在组合框上创建了一个 on selectChange 操作。

我不知道如何解决这个问题,但我已经尝试过了:

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
    'Ophalen id artikel
    Dim klantid As Integer
    klantid = ComboBox1.SelectedValue

    Dim klantidstr As String
    klantidstr = klantid.ToString

    'Via query overige gegevens ophalen
    ' OLEDB select query 
    Dim myConnection As OleDbConnection
    Dim DBpath As String = "C:\Facturatie\CharelIjs.accdb"
    Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBpath & ";Persist Security Info=True"
    myConnection = New OleDbConnection(sConnectionString)
    myConnection.Open()
    Dim SQLstr As String
    SQLstr = "SELECT * FROM tblKlant  WHERE KlantID = @id"
    Dim cmd As New OleDbCommand(SQLstr, myConnection)
    Dim da As New OleDbDataAdapter(cmd)
    Dim ds As New DataSet()

    cmd.Parameters.Add("@id", OleDbType.VarChar)
    cmd.Parameters(0).Value = klantid

    Try
        da.Fill(ds, "tblKlant")
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        Return
    End Try

    DataGridView1.DataSource = ds.Tables("tblKlant")

End Sub
4

1 回答 1

0

您可以摆脱SelectionChangeCommitted事件处理程序中的所有代码作为开始。你有一个类型DataSet,所以使用它。通过在解决方案资源管理器中双击数据集来打开数据集设计器。你应该有一个TblKlantTableAdapter适配器。右键单击它并添加一个新查询。添加过滤后的查询并命名方法FillByKlantIDGetDataByKlantID.

回到表单设计器中,添加表适配器和 BindingSource 的实例。将 绑定BindingSource到您的 tblKlantDataSet并将您的网格绑定到BindingSource. 现在,当用户进行选择时,您首先清除 中的表DataSet,然后从 中获取 IDComboBox并将其传递给对该FillByKlantID方法的调用。这将从数据库中获取过滤后的数据并填充DataTable. 因为DataTable绑定到BindingSource并且 that 绑定到网格,所以将显示数据。

于 2014-07-06T13:44:24.203 回答