Total Record Count after Filter

21 posts, 0 answers
  1. Chad Hensley
    Chad Hensley avatar
    54 posts
    Member since:
    Feb 2005

    Posted 18 Sep 2009 Link to this post

    After setting a filter, I need to get the count of the records that match the filter. 

    The footer displays it but I can't figure out how it does it.

    Thanks,

    Chad
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Sep 2009 Link to this post

    Hi Chad,

    You could use the code ' RadGrid1.MasterTableView.Items.Count ' in order to get the number of records in RadGrid. If you enabled paging, then try the following code snippet for getting the record count.

    CS:
     
    bool check = false
    protected void RadGrid1_PreRender(object sender, EventArgs e) 
        if (check) 
        { 
            RadGrid1.AllowPaging = false
            RadGrid1.Rebind(); 
            Response.Write(RadGrid1.MasterTableView.Items.Count); 
            RadGrid1.AllowPaging = true
            RadGrid1.Rebind(); 
        } 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        if (e.CommandName == RadGrid.FilterCommandName) 
        { 
            check = true
        } 
        else 
        { 
            check = false
        } 

    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Chad Hensley
    Chad Hensley avatar
    54 posts
    Member since:
    Feb 2005

    Posted 21 Sep 2009 Link to this post

    Thanks.  I actually used the following. 

        protected void rgSearch_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            GridItemType item = e.Item.ItemType;  
              
            if (rgSearch.Items.Count > 0)  
            {  
                if (e.Item is GridPagerItem)  
                {  
                    GridPagerItem pager = (GridPagerItem)e.Item;  
                    lblShowing.Text = "Displaying " + rgSearch.Items.Count.ToString() + " out of " + pager.Paging.DataSourceCount + " records that matched your query.";  
     
                }  
            }  
            else  
            {  
                lblShowing.Text = "";  
            }  
        } 

    I saw a lot of people looking for this code.  You might want to put it in your How-To for RadGrid.

    Chad
  5. Raddy Rad
    Raddy Rad avatar
    24 posts
    Member since:
    Oct 2009

    Posted 09 Oct 2009 Link to this post

    Not sure why for your code, the items.Count only returns the page size instead of the total # of records. Maybe that's why shinu is doing that in prerender and grab the total # after setting paging to false.
  6. Chad Hensley
    Chad Hensley avatar
    54 posts
    Member since:
    Feb 2005

    Posted 09 Oct 2009 Link to this post

    The items.count only returns the total records displayed on the page because of paging.   

    For my issue this solution worked fine since I have to state both the display count and total count.
  7. Katya
    Katya avatar
    11 posts
    Member since:
    Oct 2009

    Posted 14 Oct 2009 Link to this post

    Hi,
    I have the same issue. Items.Count returns only count for one page. How can I get total count without set AllowPaging to false?

    Thanx
  8. Chad Hensley
    Chad Hensley avatar
    54 posts
    Member since:
    Feb 2005

    Posted 14 Oct 2009 Link to this post

    You follow the code above (Sept 21) that doesn't use AllowPaging = false.
  9. Katya
    Katya avatar
    11 posts
    Member since:
    Oct 2009

    Posted 14 Oct 2009 Link to this post

    Thank you for reply.
    May be you also know some way to get all items after filtering (array of objects, not only count)?


  10. Chad Hensley
    Chad Hensley avatar
    54 posts
    Member since:
    Feb 2005

    Posted 14 Oct 2009 Link to this post

    You would have to be more specific on what you are trying to accomplish.
  11. Katya
    Katya avatar
    11 posts
    Member since:
    Oct 2009

    Posted 14 Oct 2009 Link to this post

    • I have some currentID
    • And I have radgrid.MasterTableView with  DataKeyNames="id"
    • I want to select grid item with value == currentID
    • So I have to define page and row for this item
    Is there any way to find it without compare each item value with currentID? May be something like hash?
    Now I try to find this item this way:
    grd.AllowPaging = false
    grd.Rebind(); 
    foreach(GridDataItem item in grd.Items) 
     if (currentID == (item.DataItem as User).id.ToString()) 
         break


    I understand that it's not the best solution =)
    Any ideas?
    Thanx
  12. Chad Hensley
    Chad Hensley avatar
    54 posts
    Member since:
    Feb 2005

    Posted 14 Oct 2009 Link to this post

    If you are talking about just getting the id of the row on rowclick, it is rg.SelectedValue. 
  13. Katya
    Katya avatar
    11 posts
    Member since:
    Oct 2009

    Posted 14 Oct 2009 Link to this post

    No, it's another problem.
    When I load the page, I want to select some item in the grid. This item must have value, which I got from the Session, (it's variable parameter).

    Thanx

  14. Chad Hensley
    Chad Hensley avatar
    54 posts
    Member since:
    Feb 2005

    Posted 14 Oct 2009 Link to this post

    The only code I have that is close, but not exactly that, is setting a session for index of the selected row, and then calling this in prerender.

    rg.MasterTableView.Items[int.Parse(Session["SelectedIndex"].ToString())].Edit = true
                    rg.MasterTableView.Rebind(); 
                    rg.MasterTableView.Items[int.Parse(Session["SelectedIndex"].ToString())].Selected = true

  15. Katya
    Katya avatar
    11 posts
    Member since:
    Oct 2009

    Posted 15 Oct 2009 Link to this post

    Hi, Chad!
    Thank you in advance, I got ideas for my current solution)
  16. Srujan
    Srujan avatar
    18 posts
    Member since:
    Oct 2010

    Posted 20 Apr 2011 Link to this post

    Hi Team,

    When I use RadGrid1.MasterTableView.Items.Count  I get value of the page size but not actual filtered result count , where as if filtered results count is less than page size then I get actaul result count .

    ex-

    Page Size =10

    Filter Result Count=12

    I get RadGrid1.MasterTableView.Items.Count  as 10

    if Filter Result Count=8

    I get RadGrid1.MasterTableView.Items.Count  as 8

    Regards
    Srujan.N
  17. Ganesh
    Ganesh avatar
    9 posts
    Member since:
    Jul 2011

    Posted 19 Jul 2011 Link to this post

    Hi srujan,

    I have the same problem with my code as you mentioned, in your post you got any way to overcome that..

    Thanks in advance
    Ganesh
  18. tiffany
    tiffany avatar
    3 posts
    Member since:
    Jan 2011

    Posted 16 Nov 2011 Link to this post

    Hi Chad,

    I have been using RadGrid with filter and playing around with it for two weeks.
    You need to change RadGrid1MasterTableView.AllowPaging, not RadGrid1.AllowPaging becuase MasterTableView is the top table in RadGrid1. Which is what we see in the user interface. Most the time we should work on MasterTableView if we want to implement the data after filter. This will do the trick.
    Hope this can help other developers if they still have this problem.

    RadGrid1.MasterTableView.AllowPaging = false;

    RadGrid1.MasterTableView.Rebind();
    LabelCount.Text=RadGrid1.MasterTableView.Itmes.Count;
    RadGrid1.MasterTableView.AllowPaging =true;

    RadGrid1.MasterTableView.Rebind();

    -Tiffany

  19. Robert
    Robert avatar
    8 posts
    Member since:
    Jul 2011

    Posted 04 Feb 2012 Link to this post

    How does this effect optimization? The whole point of paging is so you don't have to grab the whole dataset but with this approach you grab the whole set and then query again for just one page.
  20. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 08 Feb 2012 Link to this post

    Hi Robert,

    As another solution you could handle the ItemDataBound of the grid and to count the GridDataItem items.

    Regards,
    Vasil
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  21. Sebastien
    Sebastien avatar
    1 posts
    Member since:
    Oct 2013

    Posted 28 Oct 2013 Link to this post

    Could you post a small example using vbnet ?
    Thanks
  22. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2013 Link to this post

    Hi Sebastien,

    Please try the sample code snippet to get the count of filtered items.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1"
        AllowPaging="true" AllowFilteringByColumn="true" OnItemCommand="RadGrid1_ItemCommand"
        OnPreRender="RadGrid1_PreRender">
        <MasterTableView>
            <Columns>
                <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    VB:
    Private isFilter As Boolean = False
    Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
        If isFilter Then
            RadGrid1.AllowPaging = False
            RadGrid1.Rebind()
            Response.Write(RadGrid1.MasterTableView.Items.Count)
            RadGrid1.AllowPaging = True
            RadGrid1.Rebind()
        End If
    End Sub
    Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
        If e.CommandName = RadGrid.FilterCommandName Then
            isFilter = True
        Else
            isFilter = False
        End If
    End Sub

    Thanks,
    Princy
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017