在DataGridView中自动搜索
本文关键字:搜索 DataGridView | 更新日期: 2023-09-27 17:52:45
我想在TextBox
中进行自动搜索。
当我搜索字符串时,这段代码正在运行,但当我搜索整数时,这是错误:
不能对系统执行"Like"操作。Int32和System。字符串
我希望你能帮忙,因为我现在需要它。
private void textBox1_TextChanged(object sender, EventArgs e)
{
DataView DV = new DataView(dataTable);
DV.RowFilter = string.Format("OrderNo LIKE '%{0}%'",textBox1.Text);
dataGridView1.DataSource = DV;
}
不能使用LIKE
运算符来比较数字。我假设您想找到一个匹配的订单id:
DV.RowFilter = string.Format("OrderNo = {0}", textBox1.Text);
您也可以先使用int.TryParse()
来测试TextBox
中的值。
private void textBox1_TextChanged(object sender, EventArgs e)
{
int orderId;
if (!int.TryParse(textBox1.Text, out orderId))
return; // not a valid number
DataView DV = new DataView(dataTable);
DV.RowFilter = string.Format("OrderNo = {0}", orderId);
dataGridView1.DataSource = DV;
}
如果你真的想比较订单id和字符串,你必须先转换它:
DV.RowFilter
= string.Format("CONVERT(column3, System.String) LIKE '%{0}%'", textBox1.Text);