簡體   English   中英

如何使用組合框搜索記錄並在Visual Studio 2010的datagrid視圖中顯示它們?

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

我想使用組合框將數據顯示到數據網格視圖中。 假設用戶已經從兩個組合框中選擇了起點和目的地,並且當用戶單擊按鈕時,從所選起點到所選目的地的所有火車都顯示在datagrid視圖中。代碼,但是它只是將數據庫中的所有記錄顯示到數據網格視圖中。 誰能告訴我如何從組合框搜索數據庫並將其顯示到datagrid視圖? 謝謝 。 這是我的代碼

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

末級

發送到數據庫的查詢是錯誤的,您需要根據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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM