Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted

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.[^]
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900