[英]C# Datagridview filter using textbox and button
I want to filter my datagridview using click event, I'm using the following code: 我想使用click事件过滤我的datagridview,我正在使用以下代码:
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView3.DataSource;
bs.Filter = dataGridView3.Columns[0].HeaderText.ToString() + " LIKE '%" + textBox1.Text + "%'";
dataGridView3.DataSource = bs;
This works for my other datagridviews, but it shows an error for this datagridview3
: Cannot perform 'Like' operation on System.Int32 and System.String
. 这适用于我的其他datagridviews,但显示此
datagridview3
错误: Cannot perform 'Like' operation on System.Int32 and System.String
。
Here is the datagridview3 in a screenshot to show which data it is holding: 这是屏幕快照中的datagridview3,显示了它持有的数据:
I guess you have to convert the value you get from textBox1.Text
to a number. 我想您必须将从
textBox1.Text
获得的值转换为数字。 the data in the first column is numeric, and this comparing text ( string
) vs a numeric value isn't supported. 第一列中的数据为数字,并且不支持此比较文本(
string
)与数字值的比较。
Here are some ways of converting to an integer value: 以下是一些转换为整数值的方法:
int anInteger;
anInteger = Convert.ToInt32(textBox1.Text);
anInteger = int.Parse(textBox1.Text);
Once you have succesfully converted, you can adapt your code and try to see if it works that way: 成功转换之后,您可以修改代码并尝试查看其是否能以这种方式工作:
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView3.DataSource;
bs.Filter = dataGridView3.Columns[0].HeaderText.ToString() + " LIKE '%" + Convert.ToInt32(textBox1.Text) + "%'";
dataGridView3.DataSource = bs;
NOTE : Make sure to check whether the use has entered a valid number before hitting the search button ofcourse. 注意 :点击课程的搜索按钮之前,请务必检查用户是否输入了有效的号码。 Otherwise you'll still receive an error that the value couldn't be parsed/converted to a number.
否则,您仍然会收到一个错误,提示您无法将值解析/转换为数字。
Hope all of this helps you a bit more. 希望所有这些对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.