2013-07-03 49 views
0

顯示詳細形式我有一個名爲「搜索」與2個文本框,datagridview的和按鈕移到形式。當我輸入任何關鍵詞,例如我想進入第一個文本框[「txtemployee_search」]的名稱,它過濾它綁定到當我選擇名稱即時尋找員工table.so DataGridView的[dgvemployee]項目,它顯示了在第二個文本框[「txtemp_search_selection」。但我的問題是,我需要顯示或打開第二個形成含有如姓名,年齡,性別,照片,電話等詳細資料這是在第二個文本框當與名稱我點擊按鈕。即時通訊使用VB 2008和SQL Server 2005.我需要幫助PLS!下面基於文本框的值和一個按鈕

是我的代碼

Imports System.Data.SqlClient 

Public Class employee_search 
'THE CODE TO SEARCH DATAGRID WHILE TYPING INTO FIRST TEXTBOX 

Private Sub txtemployee_search_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtemployee_search.TextChanged 
    Dim keywords As String = txtemployee_search.Text 

    Dim con As SqlConnection = New SqlConnection("Data Source=oheneba;Initial Catalog=brainiac;Persist Security Info=True;User ID=sa;Password=***********") 

    ' Use wildcard 

    Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Employee WHERE Full_Name Like '%" & keywords & "%' ", con) 

    ' or Where Full_Name='" & keywords & "' 

    con.Open() 

    Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd) 

    Dim myDataSet As DataSet = New DataSet() 

    myDA.Fill(myDataSet, "Employee") 

    dgvemployee.DataSource = myDataSet.Tables("Employee").DefaultView 
    con.Close() 

End Sub 

「的代碼顯示所選的DataGridView項目插入第二個文本框

Private Sub dgvemployee_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvemployee.CellContentClick 
    Dim dgv As DataGridView = CType(sender, DataGridView) 
    Dim thisCell As DataGridViewCell = dgv.SelectedCells(0) 
    Dim selCell As Integer = thisCell.ColumnIndex 

    txtemp_search_selection.Text = dgvemployee.CurrentRow.Cells(selCell).Value.ToString() 

End Sub 
+0

觀看SQL注入。 – APrough

回答

0

的計算策略是有點不同勢,你應該選擇。 對不起,如果我錯了,但我會給你一個2格的例子。一個與客戶和一個與訂單(關係必須正確的數據庫)。所以使用2個BindingSource對象。一個與客戶,一個與訂單。 所以我們

  • CustomersBindingSource
  • OrdersBindingSource

坐落在屬性窗口

CustomersBindingSource.Datasource = yourDataset 
CustomersBindingSource.DataMember = Customers 
OrdersBindingSource.Datasource = OrdersCustomersfkBindingSource 

和有關篩選,我建議的方法是這樣的一個: CustomersBindingSource.filter =「客戶名稱,如「& txtCustomFilter 我是我現在有點匆忙......但如果你有更多的問題,我會很樂意幫助你。

+0

也加載數據集兩個表上,並確保數據集將有關係。 Me.OrdersTableAdapter.Fill(Me.DataSet1.Orders) Me.CustomersTableAdapter.Fill(Me.DataSet1.Customers) – dimis164

0

如果你想以另一種形式的額外細節,那麼你需要做的是創建一個細節的第二種形式。添加數據綁定,就像您爲datagridview所做的一樣,並導航到正確的記錄。您應該能夠將從datagridview中選擇的全名傳遞給新表單。

tIndex = Me.MyBindingSource.Find("Full_Name", keywords) 
    If tIndex = -1 Then 'could not find 
     'employee not found 
    Else 
     Me.MyBindingSource.Position = tIndex 'navigate to found record 
    End If 
0

代碼,以顯示所選的DataGridView項目插入第二個文本框

試試這個..

txtemp_search_selection.Text = dgvemployee.CurrentCell.Value 
相關問題