tags:

views:

26

answers:

2

My standalone Windows app. is written on C# 2008 and back-end is MySQL 5.1.

In a Windows Form, I have a TextBox control on top and a DataGridView below. Initially, as the Form loads, the DataGridView is filled with Item Names of products in sorted order. As and when the user starts typing few letters in the TextBox, the records in the DataGridView starts filtering.

At present a SQL query with LIKE is fired. Though there are no performance issues even after two years, but I am wondering that it is not a better way to fire this query after each key-stroke.

The alternative in my mind is to take all the Item Names in a Collection as the form loads. As the user types, I may use LINQ to filter records from the Collection.

But there is one problem. First, I want to store rows with 5 columns in the Collection, and secondly, there are so many Items, it will consume more memory to hold.

And there is a third concern also. Even if all the five columns are somehow stored in a Collection, how to populate the DataGridView each time with a new set of records when the user starts typing keywords?

+1  A: 

Why do you use additional collections (Item Names)? Use datasource of DataGridView. I think, datasource is a collection of objects or it can be datatable (it is also in-memory collection). So just use client-side filter that build-in into datasource, datatable for example or linq for datasource (collection of objects).

If your grid datasource is datatable use dataview to filter rows, otherwise (collection of objects) use LINQ.

ADDED:

DataView is a "wrapper" of DataTable. Use it to filter records. DataTable has as many as you have memory on client and less then int.MaxInt (I guess, saurabh is right).

If your product's table is too large using datatable it is not a option in this case. I recommend to use combination of client and server side filters. For example, some user can work with some category of products, so application can filter products by category on server-side and by name on client-side.

igor
How many records a DataTable or DataView can hold?
RPK
it can atleast hold Int.MaxValue of records as because count property in datatable.rows is int
saurabh
Anything that can hold any number of records?
RPK
+2  A: 

You can make use of ICollectionView to filter and sort data. Try it with the datasource of your grid.

i am sure that it will result in the more performance benefit.

do you use paging also on your grid?

See it here

saurabh
It is a Windows App.
RPK
@RPK :You can use paging in any application whether it is windows or web or mobile or ...anything , paging is a abstract concept
saurabh
@saurabh: Can it hold n number of records?
RPK
@RPK : Your grid is already having a source , you just make use of your already created source with ICollectionView , you are just creating a logical view to play with your collection
saurabh
+1 good solution
igor