Visual Studio 2010 如何使用Combo Box 搜索记录并显示在datagrid 视图中?

How to use Combo Box to search a records and display them in the datagrid view in Visual Studio 2010?

我想使用组合框将数据显示到数据网格视图中。比如说,用户从两个组合框中选择了起点和目的地,当用户单击按钮时,从所选起点到所选目的地的所有火车都会显示在数据网格视图中。我写了一个代码,但它只是将数据库中的所有记录显示到数据网格视图中。谁能告诉我如何从组合框中搜索数据库并将其显示到数据网格视图?谢谢 。这是我的代码

Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\AMEN\Documents\Railway.accdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MyConn = New OleDbConnection
    MyConn.ConnectionString = connString
    ds = New DataSet
    tables = ds.Tables
    da = New OleDbDataAdapter("Select * from Table1", MyConn)
    da.Fill(ds, "Table1")
    Dim view As New DataView(tables(0))
    source1.DataSource = view
    DataGridView1.DataSource = view

End Sub

Private Sub Table1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Me.Validate()
    Me.Table1BindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.RailwayDataSet)

End Sub

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'RailwayDataSet.Table1' table. You can move, or remove it, as needed.
    Me.Table1TableAdapter.Fill(Me.RailwayDataSet.Table1)
    'TODO: This line of code loads data into the 'RailwayDataSet.Table1' table. You can move, or remove it, as needed.
    Me.Table1TableAdapter.Fill(Me.RailwayDataSet.Table1)

End Sub

Private Sub Table1BindingNavigatorSaveItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Me.Validate()
    Me.Table1BindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.RailwayDataSet)

End Sub

结束Class

发送到数据库的查询错误,您需要根据中的组合框选择发送查询 C# 中的示例

strSQL = " Select * from Table1 where field1=  combobox1.selectedvalue and 
field2 = combobox2.selectedvalue " 
 SqlDataAdapter da = new SqlDataAdapter(sqlCmd)
 DataTable dt1 = new DataTable();
 da.Fill(dt1)
gridview.dataSource = dt1