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

Retrieving Filtered Datasource of RadGrid to a Dataset

7 Answers 551 Views
Grid
This is a migrated thread and some comments may be shown as answers.
SHERFUDEEN
Top achievements
Rank 1
SHERFUDEEN asked on 11 Apr 2011, 08:00 AM
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?

7 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 13 Apr 2011, 09:41 AM
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.

0
Deepak
Top achievements
Rank 1
answered on 21 Jul 2011, 10:49 AM
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.
0
Chamara
Top achievements
Rank 1
answered on 09 Feb 2013, 04:30 AM
I had the similar issue. Following solution solved it for me.

http://itzonesl.blogspot.com/2013/02/how-to-get-radgrid-filtered-datasource.html
0
Karl Wilkens
Top achievements
Rank 1
answered on 09 Feb 2013, 11:44 AM
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)'.

0
Chamara
Top achievements
Rank 1
answered on 09 Feb 2013, 12:29 PM
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.
0
brian
Top achievements
Rank 1
answered on 14 May 2013, 08:40 PM
This did not work compiler says 'AsEnumerable' is not a member of 'System.Data.DataTable'.
0
James
Top achievements
Rank 1
answered on 21 Jun 2013, 08:03 PM
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.
Tags
Grid
Asked by
SHERFUDEEN
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Deepak
Top achievements
Rank 1
Chamara
Top achievements
Rank 1
Karl Wilkens
Top achievements
Rank 1
brian
Top achievements
Rank 1
James
Top achievements
Rank 1
Share this question
or