Search code examples
c#asp.netteleriktelerik-grid

RadGrid Filtering


Does anyone know how to remove certain filters from a radGrid. I have the stock ones up and they vary by data type but I don't need all of them just 3 or 4 of them. I used gridfiltermenu but that gets rid of a certain filter from every column. ie: I need equals in one column but not in another. I have tried to sort through this so many ways. Here is what I have - excuse the clutter. Im have the databinded on the backend so I don't have them as individual columns on the front. I tried to get the column by column name but gridfilteringitem among other methods don't accept the names as arguments.

<telerik:RadGrid ID="gvPIOList" runat="server" 
        Class="display table table-bordered table-hover table-responsive text-center"
        CellPadding="2" HorizontalAlign="Center" AllowPaging="True"
        AllowSorting="True" GroupPanelPosition="Top" Skin="Outlook"
        ShowFooter="True" OnNeedDataSource="gvPIOList_NeedDataSource"
        MasterTableView-RowIndicatorColumn-Visible="false" 
        HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"
        AlternatingItemStyle-HorizontalAlign="Center" AllowFilteringByColumn="true"
        MasterTableView-EditFormSettings-EditColumn-FilterListOptions="VaryByDataTypeAllowCustom"
        MasterTableView-EditFormSettings-EditColumn-ShowFilterIcon="false"
        MasterTableView-EditFormSettings-EditColumn-AndCurrentFilterFunction="Contains"
        OnColumnCreated="gvPIOList_ColumnCreated" OnInit="gvPIOList_Init"
        OnItemCreated="gvPIOList_ItemCreated">
            <MasterTableView GroupLoadMode="Server" 
                EditFormSettings-EditColumn-AutoPostBackOnFilter="true"
                EditFormSettings-EditColumn-FilterDelay="1000"
                AllowFilteringByColumn="true"
                EditFormSettings-EditColumn-FilterListOptions="VaryByDataTypeAllowCustom"
                EditFormSettings-EditColumn-ShowFilterIcon="false">
            </MasterTableView>
</telerik:RadGrid>

Here is the code:

protected void gvPIOList_Init(object sender, EventArgs e)
{
    GridFilterMenu menu = gvPIOList.FilterMenu;
    int i = 0;
    while (i < menu.Items.Count)
    {
        if (menu.Items[i].Text == "NoFilter" || menu.Items[i].Text == "Contains" || menu.Items[i].Text == "Equals" || menu.Items[i].Text == "StartsWith")
        {
            i++;
        }
        else
        {
            menu.Items.RemoveAt(i);
        }
    }
}

Solution

  • this is the code I use:

    It works for different data types, you can replace the column.get_dataType() == "Sytem.Int64" by the name of the column.

    Write the values you want to keep in the in { 'NoFilter': '', 'Contains': '' } section.

    you call this function in the Client settings section of the RadGrid:

    aspx:

    <ClientSettings EnableRowHoverStyle="true">
        <ClientEvents OnFilterMenuShowing="filterMenuShowing" />
    </ClientSettings>
    <FilterMenu OnClientShowing="MenuShowing" />
    

    .js (can be placed directly in the aspx):

    var column = null;
    function MenuShowing(sender, args)
    {
    if (column == null) return;
    var menu = sender; var items = menu.get_items();
    if (column.get_dataType() == "System.String")
    {
        var i = 0;
        while (i < items.get_count())
        {
            if (!(items.getItem(i).get_value() in { 'NoFilter': '', 'Contains': '' }))
            {
                var item = items.getItem(i);
                if (item != null)
                {
                    item.set_visible(false);
                }
            }
            else
            {
                var item = items.getItem(i);
                if (item != null)
                {
                    item.set_visible(true);
                }
            }
            i++;
        }
    }
    if (column.get_dataType() == "System.Int64")
    {
        var j = 0; while (j < items.get_count())
        {
            if (!(items.getItem(j).get_value() in { 'NoFilter': '', 'EqualTo': '' }))
            {
                var item = items.getItem(j); if (item != null)
                    item.set_visible(false);
            }
            else
            {
                var item = items.getItem(j); if (item != null) item.set_visible(true);
            }
            j++;
        }
    }
    column = null;
    menu.repaint();
    }
    function filterMenuShowing(sender, eventArgs)
    {
        column = eventArgs.get_column();
    }
    

    This is taken from Telerik's website: Filter Menu Showing