Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to filter a gridview

Status
Not open for further replies.

sajidi99

Programmer
Jun 7, 2005
53
SE
Just to know some other ways of doing this ... can you please tell me how you would filter a gridview attached to linqdatasource?

i.e.

-> There is a gridview displaying Orders.
-> There is a text box for user to enter Order number and a
button to search gridview by the Order Number

How will you do the search and filter the gridview based on what is entered in the search text box?

Thanks
 
Hi sajid.
The way I handle searches tied to gridviews is to change the query. I use sqldatasource, so it may be different than your linq. What I do is set the datasource SQL query to incorporate the search item. For instance, if my original query (hard-coded in the ASPX page) displays all Orders, I use "SELECT * FROM Orders". When someone does a search, I do something (I use VB) like sdsOrder.SelectCommand = "SELECT * FROM Orders WHERE OrderNumber=" + txtSearchOrder.Text. Then I follow it with grdOrders.DataBind.

I have had one problem in the past, though. If sorting is enabled on the gridview, when somebody sorts the new view, it uses the query string from the ASPX page, not the new one I just set. To overcome this, I send the new query string to a session variable, which executes on every page load. When the page loads, the new query is used instead of the hard-coded one.

I hope this helps. I don't have access to my pages right now, so I can't promise that this is exactly what I do. Hopefully it will help you at least get on the right track. Enjoy!
 
datasource controls are the worst objects ever.
1. they are not debuggable
2. they are not testable
3. they are not flexible
4. they do not promote a clean separation of concerns. (the presentatation layer should have no knowledge of where the data comes from, only how to get the data, usually via a servicing object.)

to create search method I would pass the searchable values to a controller which converts the primiative types into some form of criteria to filter a collection. from there I would apply each filter to the collection. return the collection to code behind. bind the collection to the grid.

whether the collection comes from a database, text file, stub or web service doesn't matter, that would actually become the responsibility of another object which the controller is dependent on.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top