不能执行'Like'系统操作.Int32和System.String.DataGridView搜索和过滤

本文关键字:System String 搜索 过滤 DataGridView 操作 执行 Like 系统 不能 Int32 | 更新日期: 2023-09-27 17:49:35

我有一个表单,当我从ComboBox中选择一个列名,并在文本框中键入它过滤并显示在DataGridView中的搜索条件。当我搜索"Reference"时,它是一个int数据类型,也是标识和主键。我得到错误信息:

"无法在系统上执行'Like'操作。"Int32和System.String。"

我的代码是
DataTable dt;
private void searchForm_Load(object sender, EventArgs e)
{
    SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|'LWADataBase.sdf;");
    SqlCeDataAdapter sda = new SqlCeDataAdapter("select * from customersTBL", con);
    dt = new DataTable();
    sda.Fill(dt);
    dataGridView1.DataSource = dt;
    comboSearch.Items.Add("[Reference]");
    comboSearch.Items.Add("[First Name]");
    comboSearch.Items.Add("[Surename]");
    comboSearch.Items.Add("[Address Line 1]");
    comboSearch.Items.Add("[Address Line 2]");
    comboSearch.Items.Add("[County]");
    comboSearch.Items.Add("[Post Code]");
    comboSearch.Items.Add("[Contact Number]");
    comboSearch.Items.Add("[Email Address]");

}

private void searchTxt_TextChanged(object sender, EventArgs e)
{
    if (comboSearch.SelectedItem == null)
    {
        searchTxt.ReadOnly = true;
        MessageBox.Show("Please select a search criteria");
    }

    else
    {
        searchTxt.ReadOnly = false;
        DataView dv = new DataView(dt);
        dv.RowFilter = "" + comboSearch.Text.Trim() + "like '%" + searchTxt.Text.Trim() + "%'";
        dataGridView1.DataSource = dv;
    }
}

不能执行'Like'系统操作.Int32和System.String.DataGridView搜索和过滤

将数字转换为过滤器内的字符串:

dv.RowFilter = string.Format("CONVERT({0}, System.String) like '%{1}%'",
                             comboSearch.Text.Trim(), searchTxt.Text.Trim());

试试这个吧?

dv.RowFilter = "'%" + comboSearch.Text.Trim() + "%' like '%" + searchTxt.Text.Trim() + "%'";

可能只是缺少引号,因为查询读为

"   123 like '%123%'   "

代码试图从gridview (devexpress)搜索…

   DataRow[] dr = dt.Select("invoiceId ='" + Convert.ToInt32(gridView1.GetRowCellValue(id, "invoiceId")) + "' AND  '%" + Convert.ToDouble(gridView1.GetRowCellValue(id, "Amount_Paid")) + "%' LIKE   '%" + Convert.ToDouble(gridView1.GetRowCellValue(id, "Amount")) + "%' ");
                    if (dr.Length > 0)
                    {
                        MessageBox.Show("already paid");
                    }
相关文章: