0

我有一个名为“搜索”的表单,带有 2 个文本框、datagridview 和一个按钮。当我在第一个文本框 ["txtemployee_search"] 中输入任何关键字(例如我想要的名称)时,它会过滤绑定到员工表的 datagridview [dgvemployee] 项目。所以当我选择我要查找的名称时,它会显示在第二个文本框中[“txtemp_search_selection”]。但我的问题是,我想显示或打开第二个表格,其中包含与第二个文本框中的名称相关的姓名、年龄、性别、图片、电话等详细信息我点击按钮。我正在使用 vb 2008 和 sql server 2005。我需要帮助!

下面是我的代码

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
4

3 回答 3

0

将选定的 DATAGRIDVIEW 项目显示到第二个文本框中的代码

尝试这个 ..

txtemp_search_selection.Text = dgvemployee.CurrentCell.Value
于 2013-07-03T14:29:39.250 回答
0

您应该选择的方法有点不同。对不起,如果我错了,但我会给你一个 2 个网格的例子。一个与客户,一个与订单(关系必须在数据库中正确)。所以使用 2 个 BindingSource 对象。一种与客户有关,另一种与订单有关。所以我们有

  • 客户绑定源
  • OrdersBindingSource

在属性窗口上设置

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

关于过滤我建议的方式是这样的:CustomersBindingSource.filter = "Customername like " & txtCustomFilter 我现在有点着急.. 但如果您有更多问题,我很乐意为您提供帮助。

于 2013-07-03T11:49:14.077 回答
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
于 2013-07-03T12:16:21.413 回答