简体   繁体   中英

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.

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? 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.

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.

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:

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. I hope I was able to help. Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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