The most efficient way to search a gridview?

Last post 10-22-2007 4:04 PM by tfsmag. 11 replies.

Sort Posts:

  • The most efficient way to search a gridview?

    10-22-2007, 3:43 AM
    • Loading...
    • TJakobsen
    • Joined on 02-25-2006, 11:43 PM
    • Denmark
    • Posts 45

    Hello.

    I'm looking for the most efficient way to search a gridview.

    I got this object data source (bound to a mssql database) which returns around 1500 rows. I display the results in a gridview where the user is able to sort it on the columns and edit/delete rows. So, how do I search this gridview using a textbox and a search button? Should I apply a filterexpression to the objectdatasource and how should I do this (in the page_load, pre_render, or?) or is there another and more efficient way?

    Thanks in advance.

  • Re: The most efficient way to search a gridview?

    10-22-2007, 8:57 AM
    • Loading...
    • tfsmag
    • Joined on 01-06-2006, 5:58 PM
    • Danville, Illinois
    • Posts 486

     It depends on what methods you have available in your object. In the past I've added a method to a business object to return a dataview or datatable rather than the object, and then used the select() method from the dataview/datatable to do the search. That being said I rarely use objectdatasource via drag and drop on the design view, and I'm not entirely sure what options you have doing it that way. At any rate, I hope that pushes you in the right direction.  

    ------------------------------------------------
    Jeff Turner (simpleModus)

    Don't forget to mark the correct answer for your
    question to help out future visitors!
  • Re: The most efficient way to search a gridview?

    10-22-2007, 9:04 AM
    • Loading...
    • ScottishKevin
    • Joined on 03-20-2007, 1:42 PM
    • Glasgow, Scotland
    • Posts 32

    Hi There,

     One method I use when doing something very similiar is to have a 2nd Object data source, this would point to a 2nd query that has parameters.

    When page loads it dispaly all records, then have a onclick for the search button that changes the gridviews DataSourceID to the 2nd objectdatasource, which would then use the textbox value as the parameter. I think after that you just need to use DataBind() and this will reload the gridview with the filtered results.

     i'm sure there loads of ways todo this.

     Hope this helps

     Regards

    Kevin

     

  • Re: The most efficient way to search a gridview?

    10-22-2007, 3:17 PM
    • Loading...
    • TJakobsen
    • Joined on 02-25-2006, 11:43 PM
    • Denmark
    • Posts 45

    I've created a Data Access Layer and a Buisness Logic Layer which returns the objects that you speak of.

    Any chance you could explain to me how, a bit more detailed than above, how you've done this? Thanks in advance.

  • Re: The most efficient way to search a gridview?

    10-22-2007, 3:19 PM
    • Loading...
    • TJakobsen
    • Joined on 02-25-2006, 11:43 PM
    • Denmark
    • Posts 45

    Sounds like a way to do it yes. But isn't the FilterExpression ment to be used for situations like this or have I misunderstood that method?

  • Re: The most efficient way to search a gridview?

    10-22-2007, 3:25 PM
    • Loading...
    • tfsmag
    • Joined on 01-06-2006, 5:58 PM
    • Danville, Illinois
    • Posts 486

     microsoft's documentation on objectdatasource's filterexpression

    Not sure if it handles wildcards that you'd probably want to use in searches, but you can filter objectdatasources results using filterexpression. 

     

    you should be able to handle the filter expression in page_load, pre_init (if the expression is empty return the default full object set). Or alternatively you could handle it in your search button's click event. I don't think it will make a difference either way you do it. Personally I'd probably do the filtering in the search button's click even as a personal preference. 

    ------------------------------------------------
    Jeff Turner (simpleModus)

    Don't forget to mark the correct answer for your
    question to help out future visitors!
  • Re: The most efficient way to search a gridview?

    10-22-2007, 3:31 PM
    • Loading...
    • TJakobsen
    • Joined on 02-25-2006, 11:43 PM
    • Denmark
    • Posts 45

    It does handle wildcards and works perfect. The problem is that when you apply the filterexpression and then sort the gridview on a column, the filterexpression is no longer used and all the records is displayed. Do you know if theres a way to do such a thing? Or could you explain to me how you've done the searching?

  • Re: The most efficient way to search a gridview?

    10-22-2007, 3:49 PM
    • Loading...
    • tfsmag
    • Joined on 01-06-2006, 5:58 PM
    • Danville, Illinois
    • Posts 486
    how are you handling the postback? if your gridview is getting re-bound every post back that's probably why it's losing the filtered set. You may need to do your binding,sorting and all of that in the codebehind for a higher level of control.
    ------------------------------------------------
    Jeff Turner (simpleModus)

    Don't forget to mark the correct answer for your
    question to help out future visitors!
  • Re: The most efficient way to search a gridview?

    10-22-2007, 3:52 PM
    • Loading...
    • TJakobsen
    • Joined on 02-25-2006, 11:43 PM
    • Denmark
    • Posts 45

    I guess it re-binds the gridview on every post back.

    How can I choose not to re-bind it on post back and control the re-bind myself?

  • Re: The most efficient way to search a gridview?

    10-22-2007, 3:57 PM
    • Loading...
    • tfsmag
    • Joined on 01-06-2006, 5:58 PM
    • Danville, Illinois
    • Posts 486

     the most common way is in your page_load event

    gridview1.datasource=yourdatasource

    if not page.ispostback then

    gridview1.databind()

    end if 

     

    of course if you do it this way, you'll have to jump through some hoops to handle paging and sorting 

    ------------------------------------------------
    Jeff Turner (simpleModus)

    Don't forget to mark the correct answer for your
    question to help out future visitors!
  • Re: The most efficient way to search a gridview?

    10-22-2007, 4:00 PM
    • Loading...
    • TJakobsen
    • Joined on 02-25-2006, 11:43 PM
    • Denmark
    • Posts 45

    But is this the most efficient way then? Or is there a more prefered way to do this?

  • Re: The most efficient way to search a gridview?

    10-22-2007, 4:04 PM
    Answer
    • Loading...
    • tfsmag
    • Joined on 01-06-2006, 5:58 PM
    • Danville, Illinois
    • Posts 486

    I bind all my grids that way (in the codebehind). It provides a much higher level of control for me.

    here is an article that shows you how to handle the sorting and paging when you do it that way


     

    ------------------------------------------------
    Jeff Turner (simpleModus)

    Don't forget to mark the correct answer for your
    question to help out future visitors!
Page 1 of 1 (12 items)
Microsoft Communities
Page view counter