英文:
How to filter in datagridview on keypress in vb.net
问题
我需要在按键时过滤产品。我有以下代码,但它不起作用。怎么做呢?
Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
Me.Test_LaunchTableAdapter.Fill(Me.DataSet1.Test_Launch)
Me.DataGridView1.Visible = True
Dim dv As DataView
dv = New DataView(Me.DataSet1.Test_Launch, "(convert([Code Article], 'System.String') like '%" & TextBox3.Text & "%')", Nothing, DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
End Sub
但是我得到了这个错误:
System.NotSupportedException: 包含的 DataGridView 控件的 DataSource 属性必须设置为 BindingSource。
在这一行出现错误:
DataGridView1.DataSource = dv
请注意,我只会翻译代码部分,不会回答关于翻译的问题。
英文:
I need to filter the product on key press. I have below code but its not working. how to do it?
Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
Me.Test_LaunchTableAdapter.Fill(Me.DataSet1.Test_Launch)
Me.DataGridView1.Visible = True
Dim dv As DataView
dv = New DataView(Me.DataSet1.Test_Launch, "(convert([Code Article], 'System.String') like '%" & TextBox3.Text & "%')", Nothing, DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
End Sub
But I am getting this error
> System.NotSupportedException: The DataSource property of the containing DataGridView control must be set to a BindingSource.
in this line getting error
DataGridView1.DataSource = dv
答案1
得分: 2
你的代码有点疯狂。每次用户按下一个键时,你都会查询数据库并获取已经有的相同数据,然后在本地进行过滤。不要这样做。只查询一次数据库,将数据填充到你的DataTable
中,将其绑定到一个BindingSource
,再将其绑定到你的DataGridView
上。然后,在TextBox
的TextChanged
事件中设置BindingSource
的Filter
属性:
myBindingSource.Filter = $"[Code Article] LIKE '%{TextBox3.Text}%'"
更好的做法是,在TextChanged
事件中重新启动一个Timer
,然后在该Timer
的Tick
事件中设置Filter
。这样,当用户连续输入多个字符时,你不会一遍又一遍地进行过滤。你可以尝试调整Interval
,这样用户在停止输入后不需要等待太长时间才能看到过滤后的数据,但又足够长,以便用户可以连续输入多个字符而不会在每个字符后都进行过滤。大约300毫秒左右是一个不错的起点。我将把这个练习留给你。请注意,在TextChanged
事件中调用Timer
的Stop
和Start
方法,在Tick
事件中调用Stop
方法。
英文:
The code you have is crazy. Every time the user presses a key, you query the database and get the same data you already have, then filter it locally. Don't do that. Query the database once and populate your DataTable
, bind that to a BindingSource
and bind that to your DataGridView
. You should then set the Filter
property of your BindingSource
on the TextChanged
event of your TextBox
:
myBindingSource.Filter = $"[Code Article] LIKE '%{TextBox3.Text}%'"
Better still, restart a Timer
on the TextChanged
event and then set the Filter
on the Tick
of that Timer
. That way, you won't be filtering over and over when the user types multiple characters in a row. You can experiment with the Interval
so that the user doesn't have to wait too long after they stop typing to see the filtered data but long enough that they can type multiple characters without filtering occurring after each one. Something around 300 milliseconds is a good place to start. I'll leave that to you as an exercise. Note that you'd call Stop
and Start
on the Timer
in the TextChanged
event and then Stop
on the Tick
event.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论