locked
Reset Gridview after using FilterExpression RRS feed

  • Question

  • User-72198832 posted

    I'm trying to learn about binding a GridView to a SqlDataSource.

    My problem is as follows:

    I have gridview bound to sqldatasource and have sqldatasource.enablecaching=true.
    I am also using Paging and Column Sorting.
    All works well to this point.

    I would like to be able to search for a value in a specific column and have the gridview positioned to the closest match.

    Here's my code to do the search...

    Private Sub DataGridView_Sorting(sender As Object, e As GridViewSortEventArgs) Handles DataGridView.Sorting
          strSortExpression = e.SortExpression    'Capture current Sort Column Name
    End Sub

    Private Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
         'Search Button sets SqlDataGLMF.FilterExpression
          SqlDataGLMF.FilterExpression = strSortExpression & ">='" & txtSearch.Text & "'"
    End Sub

    This works fine if I search for "M" in name field - Grid shows Names starting with "M" but Pager shows  >  and >> preventing me from going back to previous page.
    I can get back to beginning by searching for 'blank' value. 
    Is there any other way to rebind gridview to SqlDatasource?

    Monday, February 12, 2018 5:55 AM

All replies

  • User283571144 posted

    Hi trims30,

    This works fine if I search for "M" in name field - Grid shows Names starting with "M" but Pager shows  >  and >> preventing me from going back to previous page.
    I can get back to beginning by searching for 'blank' value. 
    Is there any other way to rebind gridview to SqlDatasource?

    According to your description, I suggest you could try to create a new button named "reset" to achieve your requirement.

    In this button, you could set the FilterExpression to null.  The girdview will auto rebind the whole table.

    More details, you could refer to below codes:

    Private Sub Reset_Click(sender As Object, e As EventArgs) Handles Reset.Click
          SqlDataGLMF.FilterExpression = null
    End Sub

    Result:

    Best Regards,

    Brando

    Tuesday, February 13, 2018 2:39 AM
  • User-1716253493 posted

    Use paramater and defaultvalue, do it in aspx file

    Tuesday, February 13, 2018 3:35 AM
  • User-72198832 posted

    Brando:

    Thanks for your response...

    Your solution not working for me.

    My sample data set has 3 pages on Gridview.

    If I'm on page 2 and sorted by column "DESCR" and on button click I set SqlDataGlmf.FilterExpression = "DESCR >= '0'" I remain on same page of the Gridview.

    Setting SqlDataGlmf.FilterExpression = null as you suggested does not work because null gives error "Null is not declared. Null constant is no longer supported,"

    Tried SqlDataGlmf.FilterExpression = DBNull.Value.ToString() and that didnt go back to page1 either.

    Still looking for solution

    Lee

    Tuesday, February 13, 2018 4:22 PM
  • User283571144 posted

    Hi trims30,

    Since I don't have your codes, so I couldn't reproduce the issue on my side.

    Could you please tell me how you implement the sort and paging about your girdview?

    If you could post more details information, it will be more easily for us to reproduce the issue and find the solution.

    Best Regards,

    Brando

    Wednesday, February 14, 2018 9:37 AM