在C#中过滤未绑定的datagridview [英] Filter an unbound datagridview in c#

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

问题描述

你好,

我有一个未绑定的datagridview,在页面上有很多行;我想做的是过滤符合特定条件的行,以便减少滚动操作.

我目前的想法是将参数化查询传递给数据库(参数为过滤器),将结果放入数据表中,并将其设置为datagridview作为数据源.

应该工作正常,但也很笨拙,其他任何想法都值得赞赏.

预先感谢,Aj

Hello,

I have an unbound datagridview with lots of rows on a page; what I would like to do is filter the rows that meet a certain criteria so that I have less scrolling to do.

my current thinking is passing a parameterized query to the database (with the parameter being the filter), putting the results in a data table and setting this as the datagridview as data source.

Should work fine but is also quite very clumsy, any other ideas really appreciated.

Thanks in advance, Aj

推荐答案

我建​​议从数据库中提取所有数据,然后将其作为数据源绑定到datagridview ...然后获取筛选,可以设置DataTable.DefaultView.RowFilter属性.您给它一个字符串,该字符串基本上是SQL语句的WHERE部分.这将使您更快,因为您不必每次要过滤数据时都重新访问数据库.

此处是MSDN. [
I''d recommend pulling ALL of the data from the database, then bind it to the datagridview as a datasouce...then to get the filtering you can set the DataTable.DefaultView.RowFilter property. You give it a string that is basically the WHERE portion of an SQL statement. This will make it quicker as you don''t have to rehit the database everytime you want to filter data.

Here is the MSDN on it.[^]


Kschuler的答案可能是最好的,但是如果您绝对不愿意使用数据绑定,则可以使用Linq的Where()方法提取行集合的子集.

如果您没有使用3.5,那么就不能使用Linq,则必须使用List.FindAll()来以不同的方式获取子集.或者,在将行添加到datagridview的foreach()中,仅添加感兴趣的行.

但是数据绑定和使用datagridview自己的过滤几乎肯定是最好的方法.
Kschuler''s answer is probably the best, but if you''re absolutely dead set against using data binding, you could use Linq''s Where() method to extract a subset of your row collection.

If you''re not using 3.5 so can''t use Linq, you''ll have to get the subset a different way, perhaps with List.FindAll(). Or, in the foreach() where you add rows to the datagridview only add the rows that are of interest.

But data binding and using the datagridview''s own filtering is almost certainly the best approach.


这篇关于在C#中过滤未绑定的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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