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

default Filter Expression not working

7 Answers 720 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rayne
Top achievements
Rank 1
Rayne asked on 01 Aug 2013, 08:51 PM
I've got a grid bound to an ObjectDataSource. It has one boolean column and I'd like for the grid to be filtered on this column when the page loads.

I've tried using the FilterExpression both declaratively and in code on the page_load event, but neither works. I keep getting an error that says Expression expected. I've also tried using the NeedsDataSource event to bind the grid's datasource instead of an ObjectDataSource control but the filter expression still gives me the same error.

I'm not sure what the problem is. My expression is as follows:
([Submitted] = False)

FYI: My ObjectDataSource method returns a datatable rather than a .net collection.

7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 02 Aug 2013, 07:06 AM
Hi Rayne,

Please check this sample code snippet,where i have set initial filter for a Boolean column.Try setting EnableLinqExpressions=false and check if the error still occurs.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server"  AllowFilteringByColumn="true" EnableLinqExpressions="false" OnPreRender="RadGrid1_PreRender">
    <MasterTableView DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID" UniqueName="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="IsTrue" DataField="IsTrue" UniqueName="IsTrue"
                DataType="System.Boolean">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        RadGrid1.MasterTableView.FilterExpression = "([IsTrue] = True)";
        GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("IsTrue");
        column.CurrentFilterFunction = GridKnownFunction.EqualTo;
        column.CurrentFilterValue = "True";
        RadGrid1.MasterTableView.Rebind();
    }
}

Thanks,
Shinu
0
Rayne
Top achievements
Rank 1
answered on 02 Aug 2013, 12:49 PM
I guess I had the filtering in the wrong place. Moving it to the PreRender event worked.
Thanks.
0
Rayne
Top achievements
Rank 1
answered on 15 Aug 2013, 07:11 PM
I thought it would work but after a bit more testing it isn't working. 

PreRender is called anytime the grid is filtered by RadFilter, so whenever I try to apply a filter using a RadFilter, it is ignored and only the one in the PreRender event is applied. I want a default filter when the page loads (and it also needs to be reflected in RadFilter, because the filter is using a hidden column that won't ever be visible), but I also want the user to be able to modify that filter via ajax later; either remove it,  add to it or replace it.
0
Princy
Top achievements
Rank 2
answered on 16 Aug 2013, 05:47 AM
Hi Rayne,

Please make sure you have written the code inside if (!Page.IsPostBack),else for each postback the code will fire for PreRender.I have tried a similar scenario,to set the default filter when the page loads and it to be reflected in RadFilter,please try the following code snippet.

ASPX:
<telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadFilter1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadFilter1"></telerik:AjaxUpdatedControl>
            </UpdatedControls>
        </telerik:AjaxSetting>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1">
</telerik:RadAjaxLoadingPanel>
<div class="filterDiv">
    <telerik:RadFilter runat="server" ID="RadFilter1" FilterContainerID="RadGrid1" ShowApplyButton="false">
    </telerik:RadFilter>
</div>
<telerik:RadGrid runat="server" ID="RadGrid1"  AllowFilteringByColumn="true" EnableLinqExpressions="false"
    OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender">
    <MasterTableView IsFilterItemExpanded="false" CommandItemDisplay="Top">
        <CommandItemTemplate>
            <telerik:RadToolBar runat="server" ID="RadToolBar1" OnButtonClick="RadToolBar1_ButtonClick">
                <Items>
                    <telerik:RadToolBarButton Text="Apply filter" CommandName="FilterRadGrid" ImageUrl="<%#GetFilterIcon() %>"
                        ImagePosition="Right">
                    </telerik:RadToolBarButton>
                </Items>
            </telerik:RadToolBar>
        </CommandItemTemplate>
        <Columns>   
            <telerik:GridNumericColumn DataField="OrderID" HeaderText="OrderID" DataType="System.Int32">
            </telerik:GridNumericColumn
            <telerik:GridBoundColumn HeaderText="IsTrue" DataField="IsTrue" UniqueName="IsTrue"
                DataType="System.Boolean" Display="false">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>


C#:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
  if (e.CommandName == "FilterRadGrid")
  {
    RadFilter1.FireApplyCommand();
  }
}
  
protected string GetFilterIcon()
{
  return SkinRegistrar.GetWebResourceUrl(Page, typeof(RadGrid), "Telerik.Web.UI.Skins.Metro.Grid.Filter.gif");
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //To show the default value of the Filter in Radfilter expression
        RadFilterEqualToFilterExpression<Boolean?> expr1 = new RadFilterEqualToFilterExpression<Boolean?>("IsTrue");
        expr1.Value = true;
        RadFilter1.RootGroup.AddExpression(expr1);
 
       //To set the default filter
        RadGrid1.MasterTableView.FilterExpression = "([IsTrue] = True)";
        GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("IsTrue");
        column.CurrentFilterFunction = GridKnownFunction.EqualTo;
        column.CurrentFilterValue = "True";
        RadGrid1.MasterTableView.Rebind();     
    }
}


Thanks,
Princy
0
Rayne
Top achievements
Rank 1
answered on 16 Aug 2013, 07:44 PM
I've made some progress but it's still acting not quite how I need it to.

I can set the initial filter and the page loads with the right records displayed. If it then gets changed (using a BooleanFieldEditor), instead of showing the correct records, it shows everything. And then I can't filter it back to the original value. So if I start with false, it works on page load. but I can't filter to false or back to true. and vice versa if I start with a false value for the filter.It seems like the only trouble I'm having is with this boolean field. Other fields appear to be filtering properly.

One thing I did change is that I moved the RadToolBar outside of the grid, instead of in the CommandItem Row. Because I'm allowing grouping, I didn't like the placement of that bar with the group panel at the top. This actually helped get the ajax stuff to work how I wanted it to. But I'm stumped on how to get this boolean field to filter correctly.
0
Princy
Top achievements
Rank 2
answered on 19 Aug 2013, 09:56 AM
Hi Rayne,

I have tried the code,it works fine at my end.Below is the code I have tried.Please provide your full code if this doesn't help.

ASPX:
<telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadFilter1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadFilter1"></telerik:AjaxUpdatedControl>
            </UpdatedControls>
        </telerik:AjaxSetting>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1">
</telerik:RadAjaxLoadingPanel>
<div class="filterDiv">
    <telerik:RadFilter runat="server" ID="RadFilter1" FilterContainerID="RadGrid1" ShowApplyButton="false">
    </telerik:RadFilter>
    <telerik:RadToolBar runat="server" ID="RadToolBar1" OnButtonClick="RadToolBar1_ButtonClick">
        <Items>
            <telerik:RadToolBarButton Text="Apply filter" CommandName="FilterRadGrid" ImageUrl="<%#GetFilterIcon() %>"
                ImagePosition="Right">
            </telerik:RadToolBarButton>
        </Items>
    </telerik:RadToolBar>
</div>
<telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false"AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="true" EnableLinqExpressions="false"
 OnPreRender="RadGrid1_PreRender">
    <MasterTableView IsFilterItemExpanded="false" CommandItemDisplay="Top">
        <CommandItemTemplate>
        </CommandItemTemplate>
        <GroupByExpressions>
            <telerik:GridGroupByExpression>
                <SelectFields>
                    <telerik:GridGroupByField FieldAlias="ShipCity" FieldName="ShipCity"></telerik:GridGroupByField>
                </SelectFields>
                <GroupByFields>
                    <telerik:GridGroupByField FieldName="ShipCity" SortOrder="Descending"></telerik:GridGroupByField>
                </GroupByFields>
            </telerik:GridGroupByExpression>
        </GroupByExpressions>
        <Columns>
            <telerik:GridNumericColumn DataField="OrderID" HeaderText="OrderID" DataType="System.Int32">
            </telerik:GridNumericColumn>
            <telerik:GridDateTimeColumn DataField="OrderDate" HeaderText="OrderDate" DataFormatString="{0:MM/dd/yyyy}">
            </telerik:GridDateTimeColumn>         
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity">
            </telerik:GridBoundColumn>       
            <telerik:GridBoundColumn HeaderText="IsTrue" DataField="IsTrue" UniqueName="IsTrue"
                Display="false" DataType="System.Boolean">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void Header1_SkinChanged(object sender, SkinChangedEventArgs e)
{
    //Required for dynamic skin changing
    RadGrid1.Rebind();
}
 
protected void RadToolBar1_ButtonClick(object sender, RadToolBarEventArgs e)
{
    RadFilter1.FireApplyCommand();
}
 
protected string GetFilterIcon()
{
    return SkinRegistrar.GetWebResourceUrl(Page, typeof(RadGrid), "Telerik.Web.UI.Skins.Metro.Grid.Filter.gif");
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        RadFilterEqualToFilterExpression<Boolean?> expr1 = new RadFilterEqualToFilterExpression<Boolean?>("IsTrue");
        expr1.Value = true;
        RadFilter1.RootGroup.AddExpression(expr1);
        RadGrid1.MasterTableView.FilterExpression = "([IsTrue] = True)";
        GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("IsTrue");
        column.CurrentFilterFunction = GridKnownFunction.EqualTo;
        column.CurrentFilterValue = "True";
        RadGrid1.MasterTableView.Rebind();      
    }
}

Thanks,
Princy
0
Rayne
Top achievements
Rank 1
answered on 22 Aug 2013, 05:52 PM
I finally got it to work. For some reason, the value being returned from my stored procedure was being interpreted as a string. so in the sql select statement I had to explicitly convert it to a bit data type for the code to realize it was a boolean.
Tags
Grid
Asked by
Rayne
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Rayne
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or