无法在System.Int32和System.String上执行“Like”操作。过滤数据网格时出错

时间:2014-02-19 06:33:13

标签: vb.net datagridview

我想在这里过滤我的数据网格是我的代码

 dtSearch = dtRawMaterials.Select("RawMaterial_ID like '%" + txtSearchID.Text + "%'").CopyToDataTable

但是当我在文本框中输入一个id并单击搜索按钮时,会出现以下错误,突出显示这行代码

1 个答案:

答案 0 :(得分:0)

检查DataTable是否包含列RawMaterial_ID,如果确实如此: 对我来说,下一行代码可以正常工作:

Dim dtSearch As DataTable
dtSearch = dtRawMaterials.Select(String.Format("RawMaterial_ID LIKE '%{0}%'", txtSearchID.Text)).CopyToDataTable()

但您可以尝试其他方法 - LINQ

Dim dtSearch As DataTable
dtSearch = dtRawMaterials.AsEnumerable().Where(Function(dr) dr.Field(Of String)("RawMaterial_ID").Contains(txtSearchID.Text)).CopyToDataTable()

或多行版

Dim dtSearch As DataTable
dtSearch = (From dr As DataRow in dtRawMaterials.AsEnumerable()
            Where dr.Field(of String)("RawMaterial_ID").Contains(txtSearchID.Text)).CopyToDataTable()