RadFilter, Radgrid, Custom filter with OnNeedDataSource and FilterContainerID

7 posts, 1 answers
  1. Yeroon
    Yeroon avatar
    87 posts
    Member since:
    Oct 2012

    Posted 26 Nov 2010 Link to this post

    Hello,

    I read a couple of articles and forum threads about applying a dropdownlist (combobox) as a filter option in RadFilter.

    The samples uses a DataSource in the page though, while I want to use the OnNeedDataSource for the grid and the FilterContainerID for the RadFilter to automatically show filter field.

    Here is what I got to far, so I don't get any errors:

    <telerik:RadGrid ID="rgTasks" runat="server" OnNeedDataSource="rgTasks_OnNeedDataSource"
        EnableViewState="false" AutoGenerateColumns="false" Skin="WebBlue" PageSize="10"
        EnableLinqExpressions="true" AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="true"
        ShowStatusBar="true">
        <PagerStyle AlwaysVisible="true" Position="Top" Width="100%" />
        <MasterTableView DataKeyNames="OID" ClientDataKeyNames="OID" IsFilterItemExpanded="false">
            <SortExpressions>
                <telerik:GridSortExpression FieldName="Startdatum" SortOrder="Ascending" />
            </SortExpressions>
            <Columns>
                <telerik:GridBoundColumn DataField="OID" HeaderText="OID" Visible="false" AllowFiltering="false">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Volgnummer" HeaderText="Volgnummer" Visible="false"
                    UniqueName="Volgnummer" AllowFiltering="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Taak" HeaderText="Dossier" DataType="System.String"
                    UniqueName="Taak" AllowFiltering="false">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Status" HeaderText="Status" DataType="System.String"
                    UniqueName="Status">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Startdatum" HeaderText="Startdatum" HeaderStyle-Width="110px"
                    DataFormatString="<nobr>{0:dd/MM/yyyy}</nobr>" UniqueName="Startdatum" AllowFiltering="true">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings EnableRowHoverStyle="true">
            <ClientEvents OnRowClick="rowClick" />
            <Resizing AllowColumnResize="true" />
            <Selecting AllowRowSelect="true" />
            <DataBinding EnableCaching="true">
            </DataBinding>
        </ClientSettings>
    </telerik:RadGrid>
    <p>
        Filter toepassen:</p>
    <br />
    <telerik:RadFilter ID="radFilter" runat="server" ExpressionPreviewPosition="Bottom"
        OnFieldEditorCreating="RadFilter1_FieldEditorCreating" OnApplyExpressions="RadFilter1_ApplyExpressions"
        Localization-FilterFunctionBetween="Tussen" Localization-FilterFunctionContains="Bevat"
        Localization-FilterFunctionDoesNotContain="Bevat Niet" Localization-FilterFunctionEndsWith="Eindigt op"
        Localization-FilterFunctionEqualTo="Gelijk aan" Localization-FilterFunctionGreaterThan="Groter dan"
        Localization-FilterFunctionGreaterThanOrEqualTo="Groter dan of gelijk aan" Localization-FilterFunctionIsEmpty="Is Leeg"
        Localization-FilterFunctionIsNull="Ongedefinieerd" Localization-FilterFunctionLessThan="Kleiner dan"
        Localization-FilterFunctionLessThanOrEqualTo="Kleiner dan of gelijk aan" Localization-FilterFunctionNotBetween="Niet tussen"
        Localization-FilterFunctionNotEqualTo="Niet gelijk aan" Localization-FilterFunctionNotIsEmpty="Is niet leeg"
        Localization-FilterFunctionNotIsNull="Is niet ongedefinieerd" Localization-FilterFunctionStartsWith="Begint met"
        Localization-GroupOperationAnd="En" Localization-GroupOperationNotAnd="En niet"
        Localization-GroupOperationNotOr="Of niet" Localization-GroupOperationOr="Of"
        AddExpressionToolTip="Expressie toevoegen" AddGroupToolTip="Groepering toeveoegen"
        ApplyButtonText="Toepassen" BetweenDelimeterText="en" Culture="nl-NL" RemoveToolTip="Verwijderen"
        ShowLineImages="true" SettingsFormatter="BinaryFormatter">
        <FieldEditors>
            <custom:radfilterdropdowneditor datatextfield="Status" datavaluefield="Status" fieldname="Status"
                datatype="System.String" displayname="Status" />
        </FieldEditors>
    </telerik:RadFilter>

    Code behind:

    protected void rgTasks_OnNeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("OID", typeof(long));
            dt.Columns.Add("Taak", typeof(string));
            dt.Columns.Add("Startdatum", typeof(DateTime));
            dt.Columns.Add("Status", typeof(string));
            dt.Columns.Add("Volgnummer", typeof(int));
     
            StoredProcedure sp = new StoredProcedure("getAllAanvragen");
            sp.addParameter("@regOID", "7");
     
             
            DataSet ds = sp.executeDataSet();
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    DataRow newRow = dt.NewRow();
                    newRow["OID"] = long.Parse(row["OID"].ToString());
                    newRow["Taak"] = CommonFunctions.makeDossiernummer(row["Dossiernummer"].ToString(), row["Volgnummer"].ToString());
                    newRow["Volgnummer"] = int.Parse(row["volgnummer"].ToString());
                    newRow["Startdatum"] = DateTime.Parse(row["gestartOp"].ToString());
     
     
                    newRow["Status"] = Hive.Util.HiveEnum.getDescription((CommonEnums.AanvraagStatus)row["status"]);
                     
     
                    dt.Rows.Add(newRow);
     
                }
            }
     
            rgTasks.DataSource = dt;
             
        }
     
        protected void RadFilter1_ApplyExpressions(object sender, RadFilterApplyExpressionsEventArgs e)
        {
            RadFilterSqlQueryProvider provider = new RadFilterSqlQueryProvider();
            provider.ProcessGroup(e.ExpressionRoot);
             
        }
     
        protected void RadFilter1_FieldEditorCreating(object sender, RadFilterFieldEditorCreatingEventArgs e)
        {
            e.Editor = new RadFilterDropDownEditor();
        }


    But like this of course the dropdown doesn't show any values and only the Status filter shows.

    If I add FilterContainerID="rgTasks" to the filter of course I get all fields as I want. However when i select the Status field, I get an exception:

    Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

    This is kind of expected. the filter already is on the page, I add new one. Viewstate gets broken. Dont know the exact detail, but seems logical to me.

    My question is, is it possible to achieve what I want? So override the Status filter which is automatically generated from the FilterContainerID="rgTasks" with a custom filter (combobox containing the available values for the Status colum)

    Thanks in advance 

    /yeroon
  2. Answer
    Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 03 Dec 2010 Link to this post

    Hello Yeroon,

    I am afraid that with custom editors RadFilter does not support automatically filtering on container. You should manually apply the expressions to RadGrid. For your convenience I am attaching sample app demonstrating how to achieve this.

    Regards,
    Nikolay
    the Telerik team
    Browse the vast support resources we have to jumpstart 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. Yeroon
    Yeroon avatar
    87 posts
    Member since:
    Oct 2012

    Posted 03 Dec 2010 Link to this post

    Hi,

    Thanks for your reply. I'll implement this as per your suggestion.

    /Yeroon
  5. Paul Taylor
    Paul Taylor avatar
    41 posts
    Member since:
    Jun 2010

    Posted 23 Aug 2011 Link to this post

    Hello,

    I'm trying to implement a RadFilter with a custom FieldEditor to filter a RadGrid, following this article.

    From this discussion, I understand that it is not possible to combine custom FieldEditors with the automatic filter generation using the grid's FilterContainerID. However, I need to enable filtering for multiple columns in my grid, some of which will use standard FieldEditors and some custom ones. I have configured a RadFilter for a simple case as follows:

            <telerik:RadFilter ID="rfGrid" runat="server" OnFieldEditorCreating="rfGrid_FieldEditorCreating" OnItemCommand="rfGrid_ItemCommand"
                OnApplyExpressions="rfGrid_ApplyExpressions">
                <FieldEditors>
                    <telerik:RadFilterTextFieldEditor DisplayName="CaseNo" FieldName="ClientCode" />
                    <custom:RadFilterDropDownEditor DisplayName="Status" FieldName="CaseStatus" />
                </FieldEditors>
            </telerik:RadFilter>


    My implementation is different to the article in that I do not bind the editor dropdown using its DataSourceID, because the datasource I need to use is an Enum. Therefore I populate it in the FieldEditorCreating event, as follows:

            protected void rfGrid_FieldEditorCreating(object sender, RadFilterFieldEditorCreatingEventArgs e)
            {
                switch (e.EditorType)
                {
                    case "RadFilterDropDownEditor":
                        {
                            e.Editor = new RadFilterDropDownEditor();
                            RadFilterDropDownEditor rfdde = (RadFilterDropDownEditor)e.Editor;
                            rfdde.FieldName = "CaseStatus";
                            ListItemCollection collection = new ListItemCollection();
                            foreach (CaseStatus status in Enum.GetValues(typeof(CaseStatus)))
                            {
                                collection.Add(new ListItem(status.ToString(), ((byte)status).ToString()));
                            }
                            rfdde.DataSource = collection;
                            rfdde.DataValueField = "Value";
                            rfdde.DataTextField = "Text";
                            rfdde.InitializeEditor(rfGrid);
                            break;
                        }
                }
            }

    The FieldEditorCreating event is apparently called only for the custom editors configured in the control's FieldEditors collection; when its name is selected in the field list and also, in the case that there is only a single custom editor, when an expression is added or removed to the filter. However, the e.Editor object passed to the event is always null, so it is not possible to determine from any of its properties which FieldEditor is being created. I will need to configure several RadFilterDropDownEditor for different grid columns, each with its own list of options. How can I determine which FilterEditor is currently being created?

    Also, when the filter expression is evaluated in the ApplyExpressions event, it uses the text of the selected item in the drop down list, rather than its value. How can the value be used instead? I notice that the InitializeEditor method is fired on the custom editor several times when you select the field from the field list or when you click on the Apply filter button: once each for each of the editors defined in FieldEditors (which incidentally, seems incorrect: the e.EditorType field is always = "RadFilterDropDownEditor"), and once more. On the last occasion, there is no visible caller in the Call Stack, and the DataValueField and DataTextField properties are not populated. This results in the text being used for the value as well in the drop down, and the filter expression does not have the desired effect.

    Finally, I want to be able to remove some of the filter operator options for some of the custom editors. For example, it doesn't make sense to allow "begins with", "ends with" or "between" operators for filters based on a list of values. How can I remove those operators?

    Any help appreciated!
  6. Paul Taylor
    Paul Taylor avatar
    41 posts
    Member since:
    Jun 2010

    Posted 24 Aug 2011 Link to this post

    I have solved most of the problems I raised in this post, with the exception of customising the list of operators available for a custom editor. I'll take that up in another thread I have found where it is more relevant.
  7. Mira
    Admin
    Mira avatar
    1124 posts

    Posted 24 Aug 2011 Link to this post

    Hello Paul,

    Please use the Page_Load / Page_Init event to configure the custom editor.
    You can review the following resources for additional information:
    Adding Field Editors Dynamically
    Creating Custom Field Editors
    Custom Field Editors

    I hope this helps.

    Regards,
    Mira
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  8. Paul Taylor
    Paul Taylor avatar
    41 posts
    Member since:
    Jun 2010

    Posted 28 Aug 2011 Link to this post

    It did help, thanks!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017