Cannot perform 'Like' operation on System.Int32 and System.String. DataGridView search and filter

落花浮王杯 提交于 2019-11-27 21:50:01

问题


I have a form that when I select a column name from a ComboBox, and type in a text box it filters and displays the searched criteria in the DataGridView. When I search for "Reference" which is an int data type, which is also identity, and primary key. I get the error message :

"Cannot perform 'Like' operation on System.Int32 and System.String."

My code is

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;

    }
}

回答1:


Convert the number to a string inside the filter:

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



回答2:


Try this perhaps?

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

It may just be a missing quotation, because the query reads as

"   123 like '%123%'   "



回答3:


the code tried to search from 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");
                    }


来源:https://stackoverflow.com/questions/22328392/cannot-perform-like-operation-on-system-int32-and-system-string-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!