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

Apply specific header filter from code behind

1 Answer 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 16 Mar 2021, 06:05 PM

I have a RadGrid on my page that I use as a selector, and via it's SelectedIndexChanged I wanna set some header filter on another grid.

Each time the selection is changed in the first RadGrid (rgPendingRequests) I want to adjust the header filter of rgResponsables and filter the result. The user must then be able to change the header filter as he wish, in case the filter returns nothing.

This is the grid I wanna apply the header filter to

<telerik:RadGrid
    ID="rgResponsables"
    runat="server"
    Width="100%"
    Height="220px"
    AutoGenerateColumns="False"
    EnableViewState="true"
    AllowFilteringByColumn="True"
    AllowSorting="True"
    CssClass="gridPartageResponsables"
    EnableLinqExpressions="false"
>
    <GroupingSettings CaseSensitive="false"></GroupingSettings>
    <ClientSettings EnableRowHoverStyle="true" Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true" Scrolling-UseStaticHeaders="true" />
    <MasterTableView
        DataKeyNames="ResponsableID"  
        AllowFilteringByColumn="True"
    >
        <Columns>
            <telerik:GridBoundColumn DataField="Prenom" Visible="true" HeaderStyle-Font-Bold="true" HeaderText="Prénom" UniqueName="Prenom" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false" />
            <telerik:GridBoundColumn DataField="Nom" Visible="true" HeaderStyle-Font-Bold="true" HeaderText="Nom" UniqueName="Nom" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false" />
            <telerik:GridBoundColumn DataField="Courriel" Visible="true" HeaderStyle-Width="250px" HeaderStyle-Font-Bold="true" HeaderText="Courriel" UniqueName="Courriel" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false" />
            <telerik:GridBoundColumn DataField="Telephone" Visible="true" HeaderStyle-Width="120px" HeaderStyle-Font-Bold="true" HeaderText="Téléphone" UniqueName="Telephone" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Here is my code Behind:

Protected Sub rgPendingRequests_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles rgPendingRequests.SelectedIndexChanged
 
    Dim PrenomCol = rgResponsables.MasterTableView.GetColumnSafe("Prenom")
    PrenomCol.CurrentFilterFunction = GridKnownFunction.Contains
    PrenomCol.CurrentFilterValue = rgPendingRequests.SelectedValues("FirstName")
 
    Dim NomCol = rgResponsables.MasterTableView.GetColumnSafe("Nom")
    NomCol.CurrentFilterFunction = GridKnownFunction.Contains
    NomCol.CurrentFilterValue = rgPendingRequests.SelectedValues("LastName")
 
    rgResponsables.Rebind()
         
End Sub

But even though the values appear in the header filter, the table is not filtered until I go to a header filter field and press enter...

I've tried everything I can think of, and could not make it work...

I could really use some pointer on what I'm doing wrong...

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 19 Mar 2021, 02:43 PM

Hi Jonathan,

To be able to effectively filter the RadGrid you can either fire a filter command (How to Fire Command Events) or manipulate the filter expression manually (Operate with the FilterExpression Manually).

By firing the filter command you can only filter by one of the columns. The logic would be similar to the one below:

Protected Sub rgPendingRequests_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim PrenomCol = rgResponsables.MasterTableView.GetColumnSafe("Prenom")
    PrenomCol.CurrentFilterValue = rgPendingRequests.SelectedValues("OrderID")
    Dim filterItem As GridFilteringItem = CType(rgResponsables.MasterTableView.GetItems(GridItemType.FilteringItem)(0), GridFilteringItem)
    filterItem.FireCommandEvent("Filter", New Pair("Contains", "Prenom"))
End Sub

Manipulating the filter expression provides more flexibility but is often tricky and error-prone. For that approach please refer to the corresponding article linked above. You may also check out the implementation in the Multi-Selection RadComboBox for filtering grid code library sample

I hope that will help.

Kind regards,
Doncho
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Doncho
Telerik team
Share this question
or