逐行过滤datagridview? [英] Filter datagridview rows by word?

查看:74
本文介绍了逐行过滤datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以使用像 BindingSource.Filter 这样的属性来过滤想要的行,但是我不知道如何逐字过滤(而不是整个单元格的文本) ),例如,我的datagridview有一个名为全名的列,该列中的文本可以是A xyz,B xyz,C xyz...,我该如何过滤对于将xyz作为最后一个单词的所有行,当用户在名称中键入时,应立即应用过滤器,例如, xyz =nice name,所有具有最后一个单词的行(在列号全名)以''n'开头(输入''n''后输入''ni''''ni''后输入''''''''''''''''''''''''''''''''''''''''''''''键入,...)应该返回。由于这个要求(在输入时正确过滤),模式[全名]如''%xyz''不会完全按照我的意愿工作。



我不能对Filter属性使用完整的SQL语法,不支持非常有用的SQL函数,例如charindex,reverse,...



我们非常感谢您的帮助!



谢谢。



VipHaLong

I know that we can use some property like BindingSource.Filter to filter for wanted rows, however I don''t know how to filter for rows by word (not by the whole text of cells), for example, my datagridview has a column called Full name, the texts in that column can be "A xyz", "B xyz", "C xyz" ... , how can I filter for all rows which have xyz as the last word, the filter should be applied right when user types in a name, for example, xyz = "nice name", all the rows with the last word (in the column Full name) starting with ''n'' (after ''n'' is typed in, ''ni'' after ''ni'' is typed in, ''nic'' after ''nic'' is typed in,...) should be returned. Because of that requirement (filter right when typing in), the pattern "[Full name] like ''% xyz''" won''t work exactly as I want.

I can''t use full SQL syntax for Filter property, the very useful SQL functions for this such as charindex, reverse, ... are not supported.

Your help would be highly appreciated!

Thanks.

VipHaLong

推荐答案

您可以在过滤条件中使用赞和%,如下所示。



You can use "Like" and "%" in filter criteria as given below.

BindingSource b = (BindingSource)dataGridView1.DataSource;
b.Filter = "FullName like '%xyz%'";


查看文章:具有过滤功能的Datagridview [ ^ ]



我建议你下载这个示例应用程序(代码): http://www.microsoft.com/en-us/download/details.aspx?id=23459 [ ^ ]



是它有足够的开始吗?
See this article: Datagridview with filtering capability[^]

I recommend you to download this example application (code): http://www.microsoft.com/en-us/download/details.aspx?id=23459[^]

Is it enough for start?


可能没有更好的解决方案,我在这里有一个解决方法,这应该涉及到SQL SELECT语句。首先,我必须更改SELECT语句以包含隐藏列,此列将仅包含可见列的最后一个单词,并且在DataGridView中填充数据时此列不可见。过滤器将应用于此隐藏列而不是可见列。



以下是详细信息:



It''s likely that there isn''t a better solution, I have a workaround here and this should be involved to SQL SELECT statement. First I have to change the SELECT statement to include a hidden column, this column will contain only the last word of the visible column and this column is invisible when filling data in DataGridView. The filter will apply on this hidden column instead of the visible column.

Here are details:

SELECT REVERSE(RTRIM(SUBSTRING(REVERSE(Fullname),1,CHARINDEX(' ',REVERSE(Fullname))))) [Name] FROM MyTable





假设我有一个名为gridView的DataGridView,然后在分配其数据源后,我们制作了列''名称''不可见:





Suppose I have a DataGridView named gridView, then after assigning its datasource, we make the column ''Name'' invisible:

gridView.Columns["Name"].Visible = false;





最后,我们在隐藏列(名称)上应用过滤器:





And the last, we apply the Filter on the hidden column (Name):

myBindingSource.Filter = "[Name] like 'variableText%'";





上面的代码行将放在一些TextChanged事件处理程序中,用户将通过键入一些来更改文本文本框和过滤器将相应更改。这很好,但它不是一个直接的解决方案,它是一种解决方法,我希望有人给我直接的解决方案。

谢谢!



The line of code above will be placed in some TextChanged event handler, user will change the text by typing in some textbox and the Filter will change accordingly. This works great however it''s not a direct solution, it''s a workaround, I would like someone to give me the direct solution.
Thanks!


这篇关于逐行过滤datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆