This is a migrated thread and some comments may be shown as answers.

Total Record Count after Filter

20 Answers 535 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chad Hensley
Top achievements
Rank 1
Chad Hensley asked on 18 Sep 2009, 03:11 PM
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

20 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 21 Sep 2009, 05:30 AM
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.
0
Chad Hensley
Top achievements
Rank 1
answered on 21 Sep 2009, 03:07 PM
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
0
Raddy Rad
Top achievements
Rank 1
answered on 09 Oct 2009, 02:29 PM
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.
0
Chad Hensley
Top achievements
Rank 1
answered on 09 Oct 2009, 02:35 PM
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.
0
Katya
Top achievements
Rank 1
answered on 14 Oct 2009, 02:49 PM
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
0
Chad Hensley
Top achievements
Rank 1
answered on 14 Oct 2009, 03:02 PM
You follow the code above (Sept 21) that doesn't use AllowPaging = false.
0
Katya
Top achievements
Rank 1
answered on 14 Oct 2009, 03:19 PM
Thank you for reply.
May be you also know some way to get all items after filtering (array of objects, not only count)?


0
Chad Hensley
Top achievements
Rank 1
answered on 14 Oct 2009, 03:27 PM
You would have to be more specific on what you are trying to accomplish.
0
Katya
Top achievements
Rank 1
answered on 14 Oct 2009, 03:41 PM
  • 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
0
Chad Hensley
Top achievements
Rank 1
answered on 14 Oct 2009, 04:13 PM
If you are talking about just getting the id of the row on rowclick, it is rg.SelectedValue. 
0
Katya
Top achievements
Rank 1
answered on 14 Oct 2009, 04:21 PM
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

0
Chad Hensley
Top achievements
Rank 1
answered on 14 Oct 2009, 05:47 PM
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

0
Katya
Top achievements
Rank 1
answered on 15 Oct 2009, 12:41 PM
Hi, Chad!
Thank you in advance, I got ideas for my current solution)
0
Srujan
Top achievements
Rank 1
answered on 21 Apr 2011, 02:47 AM
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
0
Ganesh
Top achievements
Rank 1
answered on 19 Jul 2011, 06:49 AM
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
0
tiffany
Top achievements
Rank 1
answered on 16 Nov 2011, 03:44 PM

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

0
Robert
Top achievements
Rank 1
answered on 05 Feb 2012, 02:33 AM
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.
0
Vasil
Telerik team
answered on 08 Feb 2012, 12:57 PM
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 >>
0
Sebastien
Top achievements
Rank 1
answered on 28 Oct 2013, 12:43 PM
Could you post a small example using vbnet ?
Thanks
0
Princy
Top achievements
Rank 1
answered on 28 Oct 2013, 01:07 PM
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
Tags
Grid
Asked by
Chad Hensley
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Chad Hensley
Top achievements
Rank 1
Raddy Rad
Top achievements
Rank 1
Katya
Top achievements
Rank 1
Srujan
Top achievements
Rank 1
Ganesh
Top achievements
Rank 1
tiffany
Top achievements
Rank 1
Robert
Top achievements
Rank 1
Vasil
Telerik team
Sebastien
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or