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

Setting the initial filter in the code-behind

6 Answers 525 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Klein
Top achievements
Rank 1
Eric Klein asked on 02 Dec 2009, 08:52 PM
I am having an issue trying to set a default filter.
this is my grid if the user is of a certine type I want to filter the first grid and then not allow them to change the client.

 <telerik:RadGrid ID="RadGrid1" AllowFilteringByColumn="true" runat="server" GridLines="None" 
                        AllowPaging="true" AllowCustomPaging="true" AllowSorting="true" PageSize="5" OnPreRender="RadGrid1_PreRender" OnItemCreated="RadGrid1_ItemCreated" 
                        Skin="Office2007" EnableViewState="false" PagerStyle-AlwaysVisible="true">  
                        <ClientSettings> 
                            <ClientEvents OnCommand="RadGrid1_Command" OnHierarchyExpanding="RadGrid1_HierarchyExpanding" 
                                OnHierarchyCollapsing="RadGrid1_HierarchyCollapsing" /> 
                        </ClientSettings> 
                        <MasterTableView HierarchyLoadMode="Client" ClientDataKeyNames="ClientID" PagerStyle-AlwaysVisible="true">  
                            <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle> 
                            <RowIndicatorColumn> 
                                <HeaderStyle Width="10px"></HeaderStyle> 
                            </RowIndicatorColumn> 
                            <ExpandCollapseColumn Visible="True">  
                                <HeaderStyle Width="10px"></HeaderStyle> 
                            </ExpandCollapseColumn> 
                            <Columns>                                 
                                <telerik:GridBoundColumn DataField="ClientID" SortExpression="ClientID"/>  
                                <telerik:GridBoundColumn SortExpression="ClientName" DataField="ClientName" HeaderText="Client Name" HeaderStyle-Width="100%" /> 
                            </Columns> 
                            <NestedViewTemplate> 
                                <telerik:RadGrid ID="RadGrid2" AllowFilteringByColumn="true" Skin="Office2007" runat="server" 
                                    GridLines="None" AllowPaging="true" AllowSorting="true" PageSize="10" EnableViewState="false">  
                                    <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle> 
                                    <ClientSettings> 
                                        <ClientEvents OnCommand="RadGrid2_Command" /> 
                                    </ClientSettings> 
                                    <MasterTableView ClientDataKeyNames="TradeConflictID, AdvisorBlotterID, APTransactionID">  
                                        <Columns> 
                                            <telerik:GridBoundColumn SortExpression="AccountName" DataField="AccountName" HeaderText="Account Name" /> 
                                            <telerik:GridBoundColumn SortExpression="TradeDate" DataField="TradeDate" HeaderText="Trade Date" 
                                                DataType="System.DateTime" DataFormatString="{0:MM/dd/yyyy}" /> 
                                            <telerik:GridBoundColumn SortExpression="ABTransType" DataField="ABTransType" HeaderText="Transaction" /> 
                                            <telerik:GridBoundColumn SortExpression="Ticker" DataField="Ticker" HeaderText="Ticker" /> 
                                            <telerik:GridBoundColumn SortExpression="AccessPerson" DataField="AccessPerson" HeaderText="Access Person" /> 
                                            <telerik:GridBoundColumn SortExpression="APTradeDate" DataField="APTradeDate" HeaderText="Trade Date" 
                                                DataType="System.DateTime" DataFormatString="{0:MM/dd/yyyy}" /> 
                                            <telerik:GridBoundColumn SortExpression="APTransType" DataField="APTransType" HeaderText="Transaction" /> 
                                            <telerik:GridBoundColumn SortExpression="APTicker" DataField="Ticker" HeaderText="Ticker" />                                                         
                                            <telerik:GridCheckBoxColumn SortExpression ="Complete" DataField="Complete" HeaderText="Complete"  DataType="System.Boolean" />                                           
                                            <telerik:GridBoundColumn DataField="View" AllowFiltering="false" HeaderText="" AllowSorting="false" 
                                                UniqueName="View" Visible="true">  
                                            </telerik:GridBoundColumn> 
                                            <telerik:GridBoundColumn DataField="Edit" AllowFiltering="false" HeaderText="" AllowSorting="false" 
                                                UniqueName="Edit" Visible="true">  
                                            </telerik:GridBoundColumn> 
                                        </Columns> 
                                    </MasterTableView> 
                                    <PagerStyle AlwaysVisible="true" /> 
                                </telerik:RadGrid> 
                            </NestedViewTemplate> 
                        </MasterTableView> 
                        <PagerStyle AlwaysVisible="true" /> 
                        <FilterMenu EnableTheming="True">  
                            <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                        </FilterMenu> 
                    </telerik:RadGrid> 

this is the code behind
 protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)  
        {  
 
            if (e.Item is GridFilteringItem)  
            {  
                if (Session["AdvisorClientID"] != null)  
                {  
                    GridFilteringItem filterItem = (GridFilteringItem)e.Item;  
                    filterItem.Visible = false;  
                }  
            }  
        }  
        protected void RadGrid1_PreRender(object sender, System.EventArgs e)  
        {  
            if (!Page.IsPostBack)  
            {  
 
                if (Session["AdvisorClientID"] != null)  
                {  
                    RadGrid1.MasterTableView.FilterExpression = "([ClientID] = '" + Session["AdvisorClientID"].ToString() + "\' ) ";  
                    GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("ClientID");  
                    column.CurrentFilterFunction = GridKnownFunction.EqualTo;    
 
                    RadGrid1.MasterTableView.Rebind();  
                }  
            }  
        }  

WHen I run the app as a user that should be filtered I get and error  Expression expected on RadGrid1.MasterTable.Rebind().

what have I missed

6 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 02 Dec 2009, 09:08 PM
Hi Eric,

I'm not sure if this is a viable option for your requirements, but if you are not allowing the user to change which client they are looking at, then wouldn't it be faster to just pass the session variable as a parameter to the datasource of the grid? This would ensure that only the client you want them to see is there, and the grid won't have to apply the filter to a set of data returned by the datasource.

I hope this helps.
Casey
0
Eric Klein
Top achievements
Rank 1
answered on 02 Dec 2009, 09:28 PM
Well I hadn't thought of that and it does work.  But it still doesn't explain why I cann't set a default filter.
0
Casey
Top achievements
Rank 1
answered on 03 Dec 2009, 01:18 PM
Hi Eric,

It looks like Telerik has some documentation on "How to set a default filter on initial load" at the following link.
http://www.telerik.com/help/aspnet-ajax/grdapplyingdefaultfilteroninitialload.html

However, I was not able to get the filter to apply when placing the code in the NeedDataSource event (the value displayed in the filter textbox of the column, but didn't actually apply the filter).  Maybe you could get it to work, and instead of checking for !Page.IsPostBack, just check for your condition.

Telerik - any ideas as to why the code in the help documentation would not apply the actual filter in the NeedDataSource event?

protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
        {  
            if (!Page.IsPostBack)  
            {  
                RadGrid1.MasterTableView.FilterExpression = "([BOOLEAN] = \'0\') ";  
                GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("BOOLEAN");  
                column.CurrentFilterFunction = GridKnownFunction.EqualTo;  
                column.CurrentFilterValue = "0";  
            }  
            RadGrid1.DataSourceID = "SqlDataSource1";  
        } 

Casey
0
Nicolaï
Top achievements
Rank 2
answered on 05 Feb 2013, 01:25 PM
Yeah, not working here either.
http://www.telerik.com/help/aspnet-ajax/grid-applying-default-filter-on-initial-load.html

Collapse imageSetting the initial filter in the code-behind

You can use the grid's PreRender event to specify an initial filter. Note that after setting the filter, you must rebind the grid:

CopyVB.NET
    Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
        If (Not Page.IsPostBack) Then
            RadGrid1.MasterTableView.FilterExpression = "([Country] LIKE '%Germany%') "
            Dim column As GridColumn = RadGrid1.MasterTableView.GetColumnSafe("Country")
            column.CurrentFilterFunction = GridKnownFunction.Contains
            column.CurrentFilterValue = "Germany"
            RadGrid1.MasterTableView.Rebind()
        End If
    End Sub


Filter appears to be set, but the grid is not filtered.
0
Shinu
Top achievements
Rank 2
answered on 11 Feb 2013, 11:28 AM
Hi,

Unfortunately I couldn't replicate the issue. Here is my complete code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" EnableLinqExpressions="false"
    AutoGenerateColumns="true" DataSourceID="SqlDataSource1">
    <MasterTableView FilterExpression="([Country] LIKE '%Germany%')">
        <Columns>
            <telerik:GridBoundColumn CurrentFilterFunction="NoFilter" DataField="CustomerID"
                HeaderText="ID" UniqueName="CustomerID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn CurrentFilterFunction="NoFilter" DataField="CompanyName"
                HeaderText="Company" UniqueName="CompanyName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn CurrentFilterFunction="NoFilter" DataField="ContactName"
                HeaderText="Contact" UniqueName="ContactName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn CurrentFilterFunction="Contains" CurrentFilterValue="Germany"
                DataField="Country" HeaderText="Country" UniqueName="Country">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
    if (!Page.IsPostBack)
    {
        RadGrid1.MasterTableView.FilterExpression = "([Country] LIKE \'%Germany%\') ";
        GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("Country");
        column.CurrentFilterFunction = GridKnownFunction.Contains;
        column.CurrentFilterValue = "Germany";
        RadGrid1.MasterTableView.Rebind();
    }
}

Please provide your code to replicate the problem.

Thanks,
Shinu.
0
Nicolaï
Top achievements
Rank 2
answered on 11 Feb 2013, 11:42 AM
Hi,

still waiting for an explanation in a support ticket,
but I found a working workaround for this:

prerender event of grid:

if not ispostback
thegrid.MasterTableView.GetColumn("field_id").CurrentFilterValue = someValue
thegrid.MasterTableView.FilterExpression = "([field_id] = " & someValue & ")"
     rebind()
end if

ONLY for the initial load, the FilterExpression has to be set (in addition to the CurrentFilterValue )

Or the filter would appear to be set, but the grid remained unfiltered on first load..
Tags
Grid
Asked by
Eric Klein
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
Eric Klein
Top achievements
Rank 1
Nicolaï
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Share this question
or