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

Filter rows programmatically

18 Answers 1341 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Revital Keren
Top achievements
Rank 1
Revital Keren asked on 31 Aug 2008, 09:24 AM
Hi,
I'm using RadGrid with a DataTable as a DataSource.
I would like to filter the rows displayed in the grid on page_load, but I do not want the filter to be displayed to the client...
(I want to filter programmatically by using a filterValue extracted from the pages's request object.)
I've looked around forums etc and haven't found any code that shows data filtering  - only one that uses a column filter which interacts with the user.

please help... :(

thank you,
Revital

18 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Sep 2008, 06:25 AM
Hello Revital,

You can try out the following code to apply filter on page load.
cs:
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    {         
       if (!Page.IsPostBack) 
        { 
            string filterValue = Request.QueryString["filter"].ToString();            
            RadGrid1.MasterTableView.FilterExpression = "([ProductName] LIKE \'%"+ filterValue +"%\') ";  
            GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("ProductName"); 
            column.CurrentFilterFunction = GridKnownFunction.Contains; 
            RadGrid1.MasterTableView.Rebind(); 
        } 
    } 
For more information on how to apply filter on initial load, refer to this link.

And to hide the filtering item you can try the code shown below.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
       if(e.Item is GridFilteringItem) 
        { 
           e.Item.Display = false
        } 
    } 

Princy.
0
Revital Keren
Top achievements
Rank 1
answered on 01 Sep 2008, 09:19 AM
First of all thank you for your reply.
I have tried this code and now I'm getting the following exception :
Telerik.Web.UI.ParseException was caught
  Message="No property or field 'Serial' exists in type 'DataRowView'"
  Source="Telerik.Web.UI"
  Position=1
  StackTrace:
       at Telerik.Web.UI.ExpressionParser.ParseMemberAccess(Type type, Expression instance)
       at Telerik.Web.UI.ExpressionParser.ParseIdentifier()
       at Telerik.Web.UI.ExpressionParser.ParsePrimaryStart()
       at Telerik.Web.UI.ExpressionParser.ParsePrimary()
       at Telerik.Web.UI.ExpressionParser.ParseUnary()
       at Telerik.Web.UI.ExpressionParser.ParseMultiplicative()
       at Telerik.Web.UI.ExpressionParser.ParseAdditive()
       at Telerik.Web.UI.ExpressionParser.ParseComparison()
       at Telerik.Web.UI.ExpressionParser.ParseLogicalAnd()
       at Telerik.Web.UI.ExpressionParser.ParseLogicalOr()
       at Telerik.Web.UI.ExpressionParser.ParseExpression()
       at Telerik.Web.UI.ExpressionParser.ParseParenExpression()
       at Telerik.Web.UI.ExpressionParser.ParsePrimaryStart()
       at Telerik.Web.UI.ExpressionParser.ParsePrimary()
       at Telerik.Web.UI.ExpressionParser.ParseUnary()
       at Telerik.Web.UI.ExpressionParser.ParseMultiplicative()
       at Telerik.Web.UI.ExpressionParser.ParseAdditive()
       at Telerik.Web.UI.ExpressionParser.ParseComparison()
       at Telerik.Web.UI.ExpressionParser.ParseLogicalAnd()
       at Telerik.Web.UI.ExpressionParser.ParseLogicalOr()
       at Telerik.Web.UI.ExpressionParser.ParseExpression()
       at Telerik.Web.UI.ExpressionParser.Parse(Type resultType)
       at Telerik.Web.UI.DynamicExpression.ParseLambda(ParameterExpression[] parameters, Type resultType, String expression, Object[] values)
       at Telerik.Web.UI.DynamicExpression.ParseLambda(Type itType, Type resultType, String expression, Object[] values)
       at Telerik.Web.UI.GridDynamicQueryable.Where(IQueryable source, String predicate, Object[] values)
       at Telerik.Web.UI.GridDataTableFromEnumerable.FillData35()
       at Telerik.Web.UI.GridDataTableFromEnumerable.FillData()
       at Telerik.Web.UI.GridResolveEnumerable.Initialize()
       at Telerik.Web.UI.GridResolveEnumerable.EnsureInitialized()
       at Telerik.Web.UI.GridResolveEnumerable.get_DataTable()
       at Telerik.Web.UI.GridEnumerableFromDataView..ctor(GridTableView owner, DataView dataView, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields)
       at Telerik.Web.UI.GridDataSourceHelper.CreateGridEnumerable(GridTableView owner, IEnumerable enumerable, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields)
       at Telerik.Web.UI.GridDataSourceHelper.GetResolvedDataSource(GridTableView owner, Object dataSource, String dataMember, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields)
       at Telerik.Web.UI.GridTableView.get_ResolvedDataSource()
       at Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource)
       at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
       at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
       at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
       at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
       at Telerik.Web.UI.GridTableView.PerformSelect()
       at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
       at Telerik.Web.UI.GridTableView.DataBind()
       at Telerik.Web.UI.GridTableView.Rebind()
       at Reforma.Web.Application.SubsInfo.inRadGrid_PreRender(Object sender, EventArgs e) in C:\Reforma\Reforma.Web.Application\Meida\SubsInfo.aspx.cs:line 296
  InnerException:



while "Serial" is the column i'm trying to filter by.
I perform this action on 4 different tables on the page and the exception is thrown only when the table is not empty, on the Rebind() call.


please help me , I've been banging my head on this problem  for at least 12h.... :(
0
Accepted
Shinu
Top achievements
Rank 2
answered on 01 Sep 2008, 10:22 AM
Hi Revital Keren,

How do you bind the Grid? Try populating the Grid in the NeedDataSource event. Go through the following demo link to get details about AdvanceDataBinding techniques.
Advanced data-binding

Thanks
Shinu.
0
Marco Basta
Top achievements
Rank 1
answered on 02 Sep 2008, 10:14 PM
Shinu,
I am having a similar problem. My columns are autogenerated and I use NeedDataSource event to set the DataSource property of the Grid to the table returned by my data access layer.

Grid.DataSource = tbl

Then in the Pre-Render event, I added th ecode below wich generates the error below it when I add the statement: Me.RadGridCMReview.MasterTableView.FilterExpression = "([AAMC_ID] = " & header.AAMC_ID.ToString & ")"

And works fine to the point of showing the filter value on the grid column but no filtering is taking place (Grid still shows all records).

Here i sthe code in Pre-Render:
Dim col As GridBoundColumn = Me.RadGridCMReview.MasterTableView.GetColumn("AAMC_ID")

col.CurrentFilterFunction = GridKnownFunction.EqualTo

col.CurrentFilterValue = header.AAMC_ID.ToString

Me.RadGridCMReview.Rebind()

ERROR:
[PaeException: Expression expected]
   Telerik.Web.UI.ExpressionParser.ParsePrimaryStart() +120
   Telerik.Web.UI.ExpressionParser.ParsePrimary() +11
   Telerik.Web.UI.ExpressionParser.ParseUnary() +275
   Telerik.Web.UI.ExpressionParser.ParseMultiplicative() +34
   Telerik.Web.UI.ExpressionParser.ParseAdditive() +32
   Telerik.Web.UI.ExpressionParser.ParseComparison() +32
   Telerik.Web.UI.ExpressionParser.ParseLogicalAnd() +34
   Telerik.Web.UI.ExpressionParser.ParseLogicalOr() +34
   Telerik.Web.UI.ExpressionParser.ParseExpression() +22
   Telerik.Web.UI.ExpressionParser.ParseParenExpression() +128
   Telerik.Web.UI.ExpressionParser.ParsePrimaryStart() +68
   Telerik.Web.UI.ExpressionParser.ParsePrimary() +11
   Telerik.Web.UI.ExpressionParser.ParseUnary() +275
   Telerik.Web.UI.ExpressionParser.ParseMultiplicative() +34
   Telerik.Web.UI.ExpressionParser.ParseAdditive() +32
   Telerik.Web.UI.ExpressionParser.ParseComparison() +32
   Telerik.Web.UI.ExpressionParser.ParseLogicalAnd() +34
   Telerik.Web.UI.ExpressionParser.ParseLogicalOr() +34
   Telerik.Web.UI.ExpressionParser.ParseExpression() +22
   Telerik.Web.UI.ExpressionParser.Parse(Type resultType) +18
   Telerik.Web.UI.GridDynamicQueryable.Where(IQueryable source, String predicate, Object[] values) +131
   Telerik.Web.UI.GridDataTableFromEnumerable.FillData35() +2972
   Telerik.Web.UI.GridDataTableFromEnumerable.FillData() +592
   Telerik.Web.UI.GridResolveEnumerable.Initialize() +29
   Telerik.Web.UI.GridResolveEnumerable.EnsureInitialized() +17
   Telerik.Web.UI.GridEnumerableFromDataView..ctor(GridTableView owner, DataView dataView, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields) +152
   Telerik.Web.UI.GridDataSourceHelper.CreateGridEnumerable(GridTableView owner, IEnumerable enumerable, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields) +76
   Telerik.Web.UI.GridDataSourceHelper.GetResolvedDataSource(GridTableView owner, Object dataSource, String dataMember, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields) +95
   Telerik.Web.UI.GridTableView.get_ResolvedDataSource() +238
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +32
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +59
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +111
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +29
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
   Telerik.Web.UI.GridTableView.PerformSelect() +4
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
   Telerik.Web.UI.GridTableView.DataBind() +304
   Telerik.Web.UI.RadGrid.DataBind() +77
   Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason) +132
   Telerik.Web.UI.RadGrid.Rebind() +9
   _CM_Review_CMReviewCtl.Page_PreRender(Object sender, EventArgs e) +606
   System.Web.UI.Control.OnPreRender(EventArgs e) +2117788
   System.Web.UI.Control.PreRenderRecursiveInternal() +86
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2041

0
Shinu
Top achievements
Rank 2
answered on 03 Sep 2008, 05:21 AM
Hi Marco,

Do you have DataBind() anywhere in the code behind? Try using Rebind() instead of DataBind().

Shinu.
0
Yavor
Telerik team
answered on 03 Sep 2008, 05:35 AM
Hello Marco,

You can try setting the filter expression as shown in the following example.
Give this a try and let me know how it goes.

Greetings,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Marco Basta
Top achievements
Rank 1
answered on 03 Sep 2008, 04:18 PM

Telerik Team,
First, I am using Telerik.Web.UI.dll version: 2008.1.619.35
Second, the ideal example I was looking for is on an integer column where I'd like to use EqualTo filter, but I digressed to a string example with "Contains" filter to mimic your example and try to duplicate the error I was getting with the integer column.
Please follow these steps to see the error I am getting:

1- Create an ASP.NET 3.5 website using visual studio 2008
2- Paste this code inside the <form> ... </form> tags of the Default.aspx page:

<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">

<telerik:RadGrid ID="RadGridCMReview" runat="server"

AllowPaging="true" PageSize="20" PagerStyle-Position="TopAndBottom"

PagerStyle-Mode="NextPrevAndNumeric"

AllowFilteringByColumn="True" AllowSorting="True" GridLines="Both"

ShowFooter="True" ShowGroupPanel="True" Skin="Sunset"

AutoGenerateColumns="true" AllowMultiRowSelection="true" >

<MasterTableView >

<HeaderStyle width="85px" />

<Columns>

<telerik:GridClientSelectColumn Reorderable="False" UniqueName="ClientSelectColumn">

<HeaderStyle Width="25px"></HeaderStyle>

</telerik:GridClientSelectColumn>

<telerik:GridButtonColumn commandname="ViewMOAR" uniquename="View" Text="File" ButtonType="LinkButton">

<HeaderStyle Width="35px"></HeaderStyle>

</telerik:GridButtonColumn>

</Columns>

<RowIndicatorColumn Visible="False">

<HeaderStyle Width="20px"></HeaderStyle>

</RowIndicatorColumn>

<ExpandCollapseColumn Visible="False" Resizable="False">

<HeaderStyle Width="20px"></HeaderStyle>

</ExpandCollapseColumn>

</MasterTableView>

<HeaderStyle Font-Bold="True" Font-Italic="False" Font-Overline="False"

Font-Strikeout="False" Font-Underline="False" Wrap="True" />

<ClientSettings AllowColumnsReorder="True" AllowDragToGroup="True"

ReorderColumnsOnClient="True">

<Selecting AllowRowSelect="True" />

</ClientSettings>

<PagerStyle Mode="NextPrevAndNumeric" Position="TopAndBottom" />

</telerik:RadGrid>

</telerik:RadAjaxPanel>

3- Paste this code in the Default.aspx.vb (overwrite everything there):

Imports System.Data

Imports

Telerik.Web.UI

Partial

Class _Default

Inherits System.Web.UI.Page

Protected Sub RadGridCMReview_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGridCMReview.NeedDataSource

Dim dt As New System.Data.DataTable()

Dim dr As System.Data.DataRow

dt.Columns.Add(

New DataColumn("College_ID", GetType(Integer)))

dt.Columns.Add(

New DataColumn("College_Name", GetType(String)))

dt.Columns.Add(

New DataColumn("Description", GetType(String)))

dr = dt.NewRow()

dr(0) = 1

dr(1) =

"UMass"

dr(2) =

"test information"

dt.Rows.Add(dr)

dr = dt.NewRow()

dr(0) = 2

dr(1) =

"Duke University"

dr(2) =

"This is Duke University"

dt.Rows.Add(dr)

Me.RadGridCMReview.DataSource = dt

End Sub

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

If Not IsPostBack Then

'' THE FOLLOWING LINE CAUSES AN ERROR

Me.RadGridCMReview.MasterTableView.FilterExpression = "([College_Name] LIKE '%UMas%')"

Dim col As GridBoundColumn = Me.RadGridCMReview.MasterTableView.GetColumnSafe("College_Name")

col.CurrentFilterFunction = GridKnownFunction.Contains

col.CurrentFilterValue =

"UMas"

Me.RadGridCMReview.MasterTableView.Rebind()

End If

End Sub

End

Class

4- Run the project without the line that causes the error and with it.

Thanks
Marco

0
Shinu
Top achievements
Rank 2
answered on 04 Sep 2008, 06:59 AM
Hi Marco,

Have you properly set the Event handlers for the NeedDataSource and PreRender events?

ASPX:
 <telerik:radgrid id="RadGrid1"  runat="server" onneeddatasource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound" OnPreRender="RadGrid1_PreRender" > 
      


Shinu
0
Marco Basta
Top achievements
Rank 1
answered on 04 Sep 2008, 03:54 PM
Shinu,
That is weird. I have been using the grid and many of its events you mentioned for at least 3 years and never had to do it on the aspx code. Besides, the event handlers in the code behind are being hit. So I don't believe that is the issue. The issue is why the folowing line is causing the error?:

Me.RadGridCMReview.MasterTableView.FilterExpression = "([College_Name] LIKE '%UMas%')"

Did you event test my code with (or without) your recommendation?

Thanks

Marco
0
Yavor
Telerik team
answered on 05 Sep 2008, 07:40 AM
Hi Marco,

The code is correct, and does cause the exception in question.
To make sure it does not cause such an exception, please disable the linq expressions for the control:

.aspx
<telerik:RadGrid EnableLinqExpressions="false" 

This will eliminate the problem.

Greetings,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Marco Basta
Top achievements
Rank 1
answered on 05 Sep 2008, 05:41 PM
Thanks Yavor. That worked!
0
Mehdi Mirza
Top achievements
Rank 1
answered on 04 Jun 2009, 07:18 PM
I spent 3 hours trying to solve this error, 
This simple one line solution was all i needed

MM
0
Mario
Top achievements
Rank 1
answered on 06 Jan 2010, 07:04 PM
Me too, I was struggling with an "expression expected" exception

and thats all I needed:

grid.EnableLinqExpressions =

false;

Now is working, so just in case somebody else is running also into that same exception

 

0
t@rn
Top achievements
Rank 1
answered on 23 May 2011, 12:32 PM
I have same problem and this solution works for me too.
Thank you very much
0
Gene
Top achievements
Rank 1
answered on 18 Oct 2011, 10:29 PM
Can someone offer an explanation as to why changing EnableLinqExpressions="false"  fixes the problem?
0
Sebastian
Telerik team
answered on 19 Oct 2011, 08:50 AM
Hello Gene,

You can find the requested information in this documentation article (refer to the table at the bottom of it).

Best regards,
Sebastian
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Ripunjay
Top achievements
Rank 1
answered on 19 Nov 2011, 01:46 PM
Hi guys,

Is it possible to capture the click events of the filtering options?
For example, if I click 'No Filter' on the filter options, is it possible to capture the event on my server side?
I have applied filter on page load using the standard method. But on clicking 'No Filter' after the page has loaded, the grid still returns the filtered data. I need to rebind my original dataset on clicking 'No Filter'.

Please help.

Thanks,
Ripunjay
0
Jayesh Goyani
Top achievements
Rank 2
answered on 19 Nov 2011, 04:38 PM
Hello,

protected void RadGrid1_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.FilterCommandName && ((Pair)e.CommandArgument).Second == "YourName" &&
((Pair)e.CommandArgument).First != "NoFilter")
{
}
}

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Revital Keren
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Revital Keren
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Marco Basta
Top achievements
Rank 1
Yavor
Telerik team
Mehdi Mirza
Top achievements
Rank 1
Mario
Top achievements
Rank 1
t@rn
Top achievements
Rank 1
Gene
Top achievements
Rank 1
Sebastian
Telerik team
Ripunjay
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or