显示基于文本框值和按钮的详细表单

时间:2013-07-03 11:26:05

标签: vb.net sql-server-2005

我有一个名为“search”的表单,包含2个文本框,datagridview和一个buttton。当我在第一个文本框[“txtemployee_search”]中输入任何关键字,例如我想要的名称时,它会过滤datagridview [dgvemployee]项目,这些项目被绑定到employee table.so当我选择我正在寻找的名称时,它会显示出来在第二个文本框[“txtemp_search_selection”]。但我的问题是,我想要显示或打开包含名称,年龄,性别,图片,电话等详细信息的第二个表单,这些表格与第二个文本框中的名称相关WHEN我点击按钮。即时通讯使用vb 2008和sql server 2005.I需要帮助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

3 个答案:

答案 0 :(得分:0)

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

  • CustomersBindingSource
  • OrdersBindingSource

在属性窗口中设置

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

关于过滤我建议的方式是这个: CustomersBindingSource.filter =“顾客名称”& txtCustomFilter 我现在有点着急..但如果你有更多问题我会很乐意帮助你。

答案 1 :(得分: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

答案 2 :(得分:0)

  

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

试试这个..

txtemp_search_selection.Text = dgvemployee.CurrentCell.Value