DataGridView中的问题基于给定值进行排序 [英] Problem in DataGridView Sorting based on given value
本文介绍了DataGridView中的问题基于给定值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人,
我是Visual Basic的新手。面对DataGridView的问题基于TextBox的值进行排序。
我使用的代码如下:
Dim dt 作为 新 DataTable( )
dt = TryCast (DataGridView1.DataSource,DataTable)
如果 dt < span class =code-keyword> IsNot Nothing 然后
尝试
dt.DefaultView.RowFilter = LabelRef like '%TextBox2.Text%' ' JMA-JF-3057
Catch ex As 异常
MsgBox(ex.ToString)
结束 尝试
结束 如果
和输出是空白的,也没有错误。
但是当我直接在代码中提供值时,输出就可以了。
dt.DefaultView.RowFilter = LabelRef like'JMA -JF-3057'
我怎么了?
解决方案
试试dt.DefaultView.RowFilter = LabelRef like'% + TextBox2.Text + %' ' JMA-JF-3057
请注意,如果您使用日期列过滤然后语法可能取决于驱动程序(数据提供程序),您需要删除通配符,因此它将是dt。 DefaultView.RowFilter = LabelRef =' + TextBoxDate.Text + '或
dt.DefaultView.RowFilter = LabelRef =# + TextBoxDate.Text + #跨度>
Dear All,
I am new to Visual Basic. Facing Problem with DataGridView Sorting based on a value from a TextBox.
I am using code as below:
Dim dt As New DataTable()
dt = TryCast(DataGridView1.DataSource, DataTable)
If dt IsNot Nothing Then
Try
dt.DefaultView.RowFilter = "LabelRef like '%TextBox2.Text%'" 'JMA-JF-3057
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
and output is blank, no error as well.
But when I provide the value directly in code as below, output is fine.
dt.DefaultView.RowFilter = "LabelRef like 'JMA-JF-3057'"
Whats wrong with me?
解决方案
Trydt.DefaultView.RowFilter = "LabelRef like '%" + TextBox2.Text + "%'" 'JMA-JF-3057
Note if you are using a date column to filter then the syntax may depend on the driver (data provider) and you need to drop the wildcard character, so it would bedt.DefaultView.RowFilter = "LabelRef = '" + TextBoxDate.Text + "'"or
dt.DefaultView.RowFilter = "LabelRef = #" + TextBoxDate.Text + "#"
这篇关于DataGridView中的问题基于给定值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文