Retrieving Filtered Datasource of RadGrid to a Dataset

8 posts, 0 answers
  1. SHERFUDEEN
    SHERFUDEEN  avatar
    21 posts
    Member since:
    Apr 2011

    Posted 11 Apr 2011 Link to this post

    With the following code, able to retrieve data source. But if we apply any filter in the grid it is returning all the data instead of filtered.  

    protected void RadGrid1_PreRender1(object sender, EventArgs e) 
            { 
                DataSet ds = (DataSet)RadGrid1.DataSource; 
            } 

    Any Suggestions?
  2. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 13 Apr 2011 Link to this post

    Hi SHERFUDEEN,

    Unfortunately you could not get the filtered data directly from the RadGrid. The DataSource property keeps the whole data to which the RadGrid is bound. To get the filtered data you could try retrieving the filter expression from RadGrid1.MasterTableView.FilterExpression property and re-query the database. In this way, you will be able to get all the filtered data. More information about operating with the FilterExpression you could find in this documentation article.

    Regards,
    Radoslav
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Deepak
    Deepak avatar
    3 posts
    Member since:
    Apr 2011

    Posted 21 Jul 2011 Link to this post

    Means we need to hit database again.
    Is there is no way to fetch the filtered data? Or there is no event to get filtered data.
  5. Chamara
    Chamara avatar
    37 posts
    Member since:
    Jun 2011

    Posted 08 Feb 2013 Link to this post

    I had the similar issue. Following solution solved it for me.

    http://itzonesl.blogspot.com/2013/02/how-to-get-radgrid-filtered-datasource.html
  6. Karl Wilkens
    Karl Wilkens avatar
    206 posts
    Member since:
    Oct 2012

    Posted 09 Feb 2013 Link to this post

    Hi Chamara,

    That GetFilteredDatasource looks very promising but when converted to VB throws an error. Does anyone out there know why the following will not compile - 

     Private Function GetFilteredDataSource() As DataTable

            Dim DT As New DataTable()

            Dim FilteredDT As New DataTable()

            Dim filterexpression As String = String.Empty

            filterexpression = RadGrid1.MasterTableView.FilterExpression

            DT = DirectCast(RadGrid1.DataSource, DataTable)

            FilteredDT = DT.AsEnumerable.AsQueryable.Where(filterexpression).CopyToDataTable

            Return FilteredDT

        End Function


    The bolded portion shows an error - 
     Value of type 'String' cannot be converted to 'System.Linq.Expressions.Expression(Of System.Func(Of System.Data.DataRow, Boolean))'.
        Extension method 'Public Function Where(predicate As System.Func(Of System.Data.DataRow, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of System.Data.DataRow)' defined in 'System.Linq.Enumerable': Value of type 'String' cannot be converted to 'System.Func(Of System.Data.DataRow, Integer, Boolean)'.
        Extension method 'Public Function Where(predicate As System.Func(Of System.Data.DataRow, Boolean)) As System.Collections.Generic.IEnumerable(Of System.Data.DataRow)' defined in 'System.Linq.Enumerable': Value of type 'String' cannot be converted to 'System.Func(Of System.Data.DataRow, Boolean)'.

  7. Chamara
    Chamara avatar
    37 posts
    Member since:
    Jun 2011

    Posted 09 Feb 2013 Link to this post

    hi!
    You need to reference the dynamic linq class library. Download it from here and add the reference using System.Linq.Dynamic;. Let me know if didn't work.
  8. brian
    brian avatar
    1 posts
    Member since:
    Jun 2012

    Posted 14 May 2013 Link to this post

    This did not work compiler says 'AsEnumerable' is not a member of 'System.Data.DataTable'.
  9. James
    James avatar
    4 posts
    Member since:
    Jul 2011

    Posted 21 Jun 2013 Link to this post

    To reliably get the filtered rows from a RadGrid without any parsing, use the GridTableView.ResolvedDataSourceView property. This property is only accessible in the ItemDataBound event because it is a temporary view created from the filtering actions (which is then bound to the grid). Accessing this data outside the ItemDataBound event is probably more useful, so an instance member can be used to retain it (see idList and actualTable).

    Also, the grid's PageSize must be set to at least the number of filtered rows to get all the data. I set it to Int.MaxValue when filtering then rebind the grid after getting the data to return to the PageSize specified in the RadGrid markup. This is imperceptible to the user, other than perhaps a slight delay in loading the grid if the filtered dataset is very large.

    ResolvedDataSourceView contains the columns with FilterExpressions and a column called "OriginalDataItem", which is a DataRowView containing all columns from the original DataSource.

    private DataTable resolvedViewTable;
    private bool getFilteredData;
    private List<int> idList;
     
    protected void UserGrid_ItemDataBound( object sender, GridItemEventArgs e )
    {
        // No need to run this on every item, just get the data all at once when the footer is bound
        if (e.Item is GridFooterItem && getFilteredData) {
            idList = new List<int>();
            resolvedViewTable = UserGrid.MasterTableView.ResolvedDataSourceView.ToTable();
            foreach (DataRow row in resolvedViewTable.Rows){
                idList.Add((int)(row["OriginalDataItem"] as DataRowView).Row["UserID"]);
                // All columns in the view could be copied to a new table with the same structure as the DataSource:
                //var newRow = actualTable.NewRow();
                //newRow["FirstName"] = (row["OriginalDataItem"] as DataRowView).Row["FirstName"];
                //newRow["LastName"] = (row["OriginalDataItem"] as DataRowView).Row["LastName"];
                //...
            }
        }
    }
     
    protected void UserGrid_ItemCommand( object sender, GridCommandEventArgs e )
    {
        if (e.CommandName == RadGrid.FilterCommandName || e.CommandName == RadGrid.HeaderContextMenuFilterCommandName) {
            getFilteredData = true;
            UserGrid.PageSize = int.MaxValue;
        }
    }
     
    protected void UserGrid_DataBound( object sender, EventArgs e )
    {
        if (getFilteredData) {
            // Do something with the retrieved view data
            //foreach (int i in idList) {
            //...
            //}
            getFilteredData = false;
            UserGrid.Rebind();
        }
    }

    This is not a fully working example, just an outline of the process involved.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017