1

我有一个DataGridview显示数据的一个TextBox,一个允许我BindingSource使用 SQL 查询过滤以根据输入字符串显示数据的一个。这一切工作正常,除了一旦我过滤了我拥有DataGridView的计时器功能将它重置回来,所有数据都将再次显示。计时器是1000ms基于基础设置的,因此它将显示过滤结果一秒钟,然后恢复。

这是我的代码:

Imports System.Data.OleDb

Public Class Form1

    Dim duraGadgetDB As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Dave\Documents\duraGadget.mdb;"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sql As String = "SELECT * FROM duragadget"
        Dim connection As New OleDbConnection(duraGadgetDB)
        Dim dataadapter As New OleDbDataAdapter(sql, connection)
        Dim ds As New DataSet()
        connection.Open()
        dataadapter.Fill(ds, "dura")
        connection.Close()
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "dura"
        DataGridView1.Columns(5).Width = 300 
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        insert.Show()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim currentRowID As Integer
        Dim scrollPosition As Integer = DataGridView1.FirstDisplayedScrollingRowIndex
        Try
            If DataGridView1.CurrentRow IsNot Nothing Then

                currentRowID = DataGridView1.CurrentRow.Index

                Dim sql As String = "SELECT * FROM duragadget"
                Dim connection As New OleDbConnection(duraGadgetDB)
                Dim dataadapter As New OleDbDataAdapter(sql, connection)
                Dim ds As New DataSet()
                connection.Open()
                dataadapter.Fill(ds, "dura")
                connection.Close()
                DataGridView1.DataSource = ds
                DataGridView1.DataMember = "dura"
                DataGridView1.CurrentCell = DataGridView1.Item(1, currentRowID)
            End If
        Catch ex As Exception

        End Try
        DataGridView1.FirstDisplayedScrollingRowIndex = scrollPosition
    End Sub

    Private Sub txtSearchOnSku_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearchOnSku.TextChanged
        Dim currentRowID As Integer
        Dim scrollPosition As Integer = DataGridView1.FirstDisplayedScrollingRowIndex
        Try
            If DataGridView1.CurrentRow IsNot Nothing Then
                currentRowID = DataGridView1.CurrentRow.Index
                Dim sql As String = "SELECT * FROM duragadget"
                Dim connection As New OleDbConnection(duraGadgetDB)
                Dim dataadapter As New OleDbDataAdapter(sql, connection)
                Dim ds As New DataSet()
                Dim dsView As New DataView
                Dim bs As New BindingSource()
                connection.Open()
                dataadapter.Fill(ds, "dura")
                connection.Close()
                dsView = ds.Tables(0).DefaultView
                bs.DataSource = dsView
                bs.Filter = "skuNo LIKE'" & txtSearchOnSku.Text & "*'"
                DataGridView1.DataSource = bs
                DataGridView1.CurrentCell = DataGridView1.Item(1, currentRowID)
            End If
        Catch ex As Exception
        End Try
        DataGridView1.FirstDisplayedScrollingRowIndex = scrollPosition
    End Sub
End Class

谁能告诉我如何阻止这种情况发生?

4

1 回答 1

0

假设您的意思是,如果您在文本框中输入了一个值来过滤结果,那么您不希望计时器触发并取消过滤它们......

您可以在 Timer1_Tick 例程中检查 TextBox 的内容;

If txtSearchOnSku.Text <> "" Then Exit Sub

或者您可以在 txtSearchOnSku_TextChanged 例程中禁用计时器;

If txtSearchOnSku.Text <> "" Then 
   Timer1.Stop
Else
   Timer1.Start
End If

或者,如果您想根据您的搜索每秒更新一次结果,您可以在 Timer1_Tick 例程中包含过滤代码。

快速点,那里有很多重复的代码。将重复的代码重构到另一个子中可能是值得的。

于 2013-01-05T00:33:05.953 回答