简体   繁体   English

如何过滤datagridview列

[英]How to filter datagridview column

For example I have a datagridview1 with data imported from a text file and there are 3 columns: ID, Name, Gender. 例如,我有一个datagridview1,数据是从文本文件导入的,共有3列:ID,名称,性别。

What I want to do is to select/show only all with the Male in Gender column. 我要做的是仅选择/显示“性别中的男性”列中的所有内容。

I dont have any database here so I can't use sql queries or is there a way to manipulate datagridview using sql queries? 我这里没有任何数据库,所以我不能使用sql查询,或者有没有办法使用sql查询来操纵datagridview? Just like this: 像这样:

SELECT * FROM DataGridView1 WHERE Gender='Male'

Any response would really be appreciated. 任何回应将不胜感激。

I am C# developer, don't know if there are any built in functions in VB to do this, but the simplest way I think would be to just loop through datagridview and filter manually, you can then either remove that row or make in invisible. 我是C#开发人员,不知道VB中是否有任何内置函数可以执行此操作,但是我认为最简单的方法是循环遍历datagridview并手动进行过滤,然后可以删除该行或将其设置为不可见。

You can implement basic search this way too. 您也可以通过这种方式实现基本搜索。 Of course this is not the fastest nor the most effective way to do it, but it will get the job done. 当然,这不是最快也不是最有效的方法,但是它将完成工作。 Alternatively you can store all data in a list or an array to make the loop run faster. 或者,您可以将所有数据存储在列表或数组中,以使循环运行更快。 Also to improve performance you can hide datagrid when loop is started and show it again when loop is finished, that way system won't waste time on showing datagrid animations. 为了提高性能,您可以在循环开始时隐藏datagrid,并在循环结束时再次显示它,这样系统就不会在显示datagrid动画上浪费时间。

For example you you have a number column and you want to show rows that have values between 50-100, simple code will look something like this: 例如,您有一个数字列,并且想要显示值在50到100之间的行,简单的代码将如下所示:

for(int i=0; i<datagridview1.rows.count; i++)
{
   int t = Convert.toint32(datagridview.rows[i].cells[0].value);
   if(t<50 || t>100) datagridview.rows[i].visible = false;
   else datagridview.rows[i].visible = true;
}

Code is in C# and will need some spellchecking but I think you got the idea. 代码是C#语言,需要进行拼写检查,但是我想您已经明白了。 I hope I was able to help. 希望我能提供帮助。 Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM