我的代码:
Dim dt As New DataTable
dt.Columns.Add("No")
dt.Columns.Add("Col2")
Dim dr = dt.NewRow
dr("No") = 1
dr("Col2") = "abc"
dt.Rows.Add(dr)
Dim strText="abcdes"
For Each drow As DataRow In dt.Select(String.Format("'{0}' like '%' & Col2 & '%'", strText))
//value of Col2 exist in strText
Next
它显示错误:
{“表达式包含不受支持的运算符'&'。”} System.Data.EvaluateException
如何在DataTable DotNet中选择类似列的值?
您应使用+
代替&
并更正以下格式LIKE
:
String.Format(" '{0}' LIKE '%' + Col2 + '%' ", strText)
对于每一行,这会检查其中是否Col2
包含strText
返回的行。
要检查是否Col2
包含strText
,应使用String.Format("Col2 LIKE '%{0}%'", strText)
。
范例:
Dim foundRows() As DataRow
foundRows= dt.Select(String.Format(" '{0}' LIKE '%' + Col2 + '%' ", strText))
For Each dr As DataRow In foundRows
'Do something
Next
要了解有关表达式语法的更多信息:
笔记:
如果不是获取的列表DataRow
,则需要过滤DataTable
,例如,仅显示过滤的行DataGridView
,请使用:
Me.DataGridView1.DataSource = dt
dt.DefaultView.RowFilter = String.Format("Col2 LIKE '%{0}%'", strText)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句