【问题标题】:C# Datagridview filter using textbox and buttonC# Datagridview 过滤器使用文本框和按钮
【发布时间】:2017-02-26 16:13:49
【问题描述】:

我想使用点击事件过滤我的datagridview,我正在使用以下代码:

BindingSource bs = new BindingSource();
        bs.DataSource = dataGridView3.DataSource;
        bs.Filter = dataGridView3.Columns[0].HeaderText.ToString() + " LIKE '%" + textBox1.Text + "%'";
        dataGridView3.DataSource = bs;

这适用于我的其他数据网格视图,但它显示此 datagridview3:Cannot perform 'Like' operation on System.Int32 and System.String 的错误。

这是截图中的datagridview3,以显示它保存的数据:

【问题讨论】:

  • 您能否添加更多信息,例如:关于您的datagridview 中有哪些数据和数据类型 - 如果可能的话,我们可以在其中克隆您的屏幕截图或存储库应用程序。
  • 所以这是为datagridview imgur.com/a/0ZYev
  • 哦,成功了!谢谢你UUUUUUUUUUUUUU..

标签: c# search datagridview


【解决方案1】:

我猜你必须将从textBox1.Text 获得的值转换为数字。第一列中的数据是数字,不支持将文本 (string) 与数值进行比较。

以下是转换为整数值的一些方法:

int anInteger;
anInteger = Convert.ToInt32(textBox1.Text);
anInteger = int.Parse(textBox1.Text);

成功转换后,您可以调整您的代码并尝试看看它是否可以这样工作:

BindingSource bs = new BindingSource();
        bs.DataSource = dataGridView3.DataSource;
        bs.Filter = dataGridView3.Columns[0].HeaderText.ToString() + " LIKE '%" + Convert.ToInt32(textBox1.Text) + "%'";
        dataGridView3.DataSource = bs;

注意:在点击搜索按钮之前,请务必检查用户是否输入了有效数字。否则,您仍然会收到无法将值解析/转换为数字的错误。

希望所有这些对您有所帮助。

【讨论】:

  • 谢谢.. 现在可以使用了.. 我只是将结构更改为 VARCHAR 我无法搜索它,因为它在我的数据库中是 INT..
  • 是的,一定还有别的东西.. 自从我使用 dataGridView 和它的过滤方法以来,我已经很长时间了.. 也许可以尝试查看关于 SO 的其他问题,甚至是这个特定的问题:@ 987654321@
  • 哦,好的,很高兴;)!这就是为什么当您发布问题时我们需要尽可能多的信息。从您的代码到屏幕截图到数据库结构(我忘了问)。很高兴它起作用了,很乐意提供帮助。祝您编码愉快;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 2011-07-14
相关资源
最近更新 更多