C# System.Data.EvaluateException:“无法对 System.Int64 和 System.String 执行 'Like' 操作。”我正在寻找解决您的错误的方法

C# System.Data.EvaluateException: 'Cannot perform 'Like' operation on System.Int64 and System.String.' I'm looking for a solution to your error please

当我在文本 box.However 中输入文本时,此方法有效,当“else if”块运行时,它给出了我在 header.What 中指定的错误,我可以做些什么来解决这个问题? 该问题仅发生在“else if”块中。此问题在其他 if、else 等块中不起作用。

        private void txt_box_urun_ara_TextChanged(object sender, EventArgs e)
    {
        if (urun_isim_radio.Checked == true)
        {
            (tablo_urunler.DataSource as DataTable).DefaultView.RowFilter = string.Format("UrunAdi LIKE '{0}%'", txt_box_urun_ara.Text);
        }
        else if (urun_id_radio.Checked == true)
        {
            (tablo_urunler.DataSource as DataTable).DefaultView.RowFilter = string.Format("UrunID LIKE '{0}%'", txt_box_urun_ara.Text);
        }
        else {
            urun_isim_radio.Checked = true;
            (tablo_urunler.DataSource as DataTable).DefaultView.RowFilter = string.Format("UrunAdi LIKE '{0}%'", txt_box_urun_ara.Text);

        }

根据你的代码,我重现了你的问题。我们可以用Convert(ID, System.String) LIKE对int类型进行类似的操作

我做了一个代码示例,你可以参考一下

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if(radioButton1.Checked==true)
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Convert(Adi, System.String) LIKE '{0}%'", textBox1.Text);
        }
        else if (radioButton2.Checked == true)
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Convert(ID, System.String) LIKE '{0}%'",textBox1.Text);
        }
    }
   
    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Adi", typeof(int));
        dt.Columns.Add("ID", typeof(int));
        dt.Rows.Add(123,1001);
        dt.Rows.Add(124,1002);
        dt.Rows.Add(125, 1003);
        dt.Rows.Add(135, 1004);
        dt.Rows.Add(167, 1101);
        dt.Rows.Add(196, 1102);
        dt.Rows.Add(193, 1103);
        dt.Rows.Add(186, 1104);
        this.dataGridView1.DataSource = dt;
    }

结果: