如何在vb.net中使用一个TextBox的绑定源筛选器并显示在另一个TextBox中

huangapple go评论66阅读模式
英文:

How to use binding source filter from one TextBox and Show to another TextBox in vb.net

问题

我有2个文本框,分别是TextBoxITC和TextBoxITM。所以我在TextBoxITC中创建了一个键盘按下事件,所以我想在textboxITM中显示“ITM”列的结果。

谢谢

英文:

I have 2 Textboxes namely TextBoxITC and TextBoxITM. So I've created a keydown event in TextBoxITC so I want to show the result of "ITM" Column in textboxITM.

Thanks

 Dim source1 As New BindingSource()
 Private Sub PopulateDatatable()
        Try
            Dim query As String = "Select ITC,ITM FROM IFG WHERE GDN = 'A.04.01.002.001'"
            Using con As OleDbConnection = New OleDbConnection(cn)
                Using cmd As OleDbCommand = New OleDbCommand(query, con)
                    Using da As New OleDbDataAdapter(cmd)
                        Dim dt As DataTable = New DataTable()
                        da.Fill(dt)
                        da.Dispose()
                        source1.DataSource = dt
                    End Using
                End Using
            End Using
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub
 Private Sub TextBoxITC_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBoxITC.KeyDown
        If e.KeyCode = Keys.Back Then
            source1.Filter = ""
            TextBoxITC.Clear()
            clearData()
        Else
        End If
        If e.KeyCode = Keys.Enter Then
            source1.Filter = "ITC = '" & Replace(TextBoxITC.Text, "'", "") & "'"

            If source1.Count <= 0 Then
                source1.Filter = ""
                TextBoxITC.Clear()
                MsgBox("No Result Found!", MsgBoxStyle.Exclamation)
            End If
        End If
    End Sub

答案1

得分: 0

以下是您要翻译的代码部分:

 Private Sub TextBoxITC_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBoxITC.KeyDown
        If e.KeyCode = Keys.Back Then
            source1.Filter = ""
            TextBoxITC.Clear()
            clearData()
        Else
        End If
        If e.KeyCode = Keys.Enter Then
              Using con As OleDbConnection = New OleDbConnection(cn)
                con.Open()
                Dim query As String = "Select ITM FROM IFG WHERE ITC = ? AND GDN = 'A.04.01.002.001'"
                Using command As OleDbCommand = New OleDbCommand(query, con)

                    command.Parameters.Add("ITC", OleDbType.VarChar).Value = TextBoxITC.Text
                    Dim result As Object = command.ExecuteScalar()
                    If result IsNot Nothing Then
                        TextBoxolditem.Text = result.ToString()
                    Else
                        TextBoxolditem.Text = "Number Not found!"
                    End If
                    con.Close()
                End Using
            End Using
        End If
    End Sub

希望这有所帮助。如果您需要任何进一步的翻译,请告诉我。

英文:

Here's a link! and Answer from @Steve

 Private Sub TextBoxITC_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBoxITC.KeyDown
        If e.KeyCode = Keys.Back Then
            source1.Filter = ""
            TextBoxITC.Clear()
            clearData()
        Else
        End If
        If e.KeyCode = Keys.Enter Then
              Using con As OleDbConnection = New OleDbConnection(cn)
                con.Open()
                Dim query As String = "Select ITM FROM IFG WHERE ITC = ? AND GDN = 'A.04.01.002.001'"
                Using command As OleDbCommand = New OleDbCommand(query, con)

                    command.Parameters.Add("ITC", OleDbType.VarChar).Value = TextBoxITC.Text
                    Dim result As Object = command.ExecuteScalar()
                    If result IsNot Nothing Then
                        TextBoxolditem.Text = result.ToString()
                    Else
                        TextBoxolditem.Text = "Number Not found!"
                    End If
                    con.Close()
                End Using
            End Using
        End If
    End Sub

huangapple
  • 本文由 发表于 2023年6月8日 11:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定