locked
Suppose a sqlDatasource has a filter, such as "firstname like Fred%". How do find the number of rows obtained? RRS feed

  • Question

  • User297437924 posted

    Normally, if I want to know the number of rows obtained by a sqldatasource, I use code such as:

      Protected Sub SqlDataSourcePublic_Selected(sender As Object, e As SqlDataSourceStatusEventArgs) Handles SqlDataSourcePublic.Selected
            If e.AffectedRows > GridViewPublic.PageSize Then
                ' do something
            End If
        End Sub

    However, suppose that the sql data source has a filter such as:

    sqlDataSourcePublic.FilterExpression = _paramfield & " LIKE '{0}%'"

    So there might 100 first names, but the filter is set to only those names that start with 'f' (Fred, Frank, Fiona).  so there are only 3 records displayed now.  How does my code detect the '3'?   I could count gridview rows, but the trouble with that is if I use paging and set a limit of ten rows at a time, and there are twelve rows in total, then the gridview count gives me 10 and not 12.

    Thanks.

    Thursday, September 22, 2016 11:28 PM

Answers

  • User1724605321 posted

    Hi RateFor ,

    How does my code detect the '3'? 

    The Selecting event will not be raised when you use FilterExpressions , because the Filter expressions filters the data on the server and not in the Database level.

    I could count gridview rows, but the trouble with that is if I use paging and set a limit of ten rows at a time, and there are twelve rows in total, then the gridview count gives me 10 and not 12.

    You could try GridView RowCreated event, which will get the number of rows found (row count) that match the search keyword . You could refer to below article for code sample :

    http://www.encodedna.com/gridview/tutorial/search-records-in-gridview-using-filterparameters.htm 

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2016 5:42 AM