Using a CommandItemTemplate to filter an entire Radgrid with 1 searchbar

2 posts, 0 answers
  1. William
    William avatar
    14 posts
    Member since:
    Oct 2018

    Posted 21 Nov 2018 Link to this post

    I have the radgrid and commandItemTemplate as follows:

    <telerik:RadGrid RenderMode="Lightweight" runat="server" ID="calendarTableView" OnItemCommand="ItemCommand" >
        <MasterTableView AutoGenerateColumns="false" TableLayout="Fixed" CommandItemDisplay="Top">
            <CommandItemTemplate>
                <asp:TextBox ID="txtFilter" runat="server"></asp:TextBox>
                <asp:Button ID="btnFilter" runat="server" Text="Apply Filter" CommandName="FilterAll" />
                <asp:Button ID="btnClear" runat="server" Text="Clear Filter" CommandName="ClearFilter" />
            </CommandItemTemplate>
        </MasterTableView>
    </telerik:RadGrid>

    and in the vb file I have

    Protected Sub ItemCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs)
        Dim filterExpression As String = String.Empty
        If e.CommandName = "FilterAll" Then
            Dim cmdItem As GridCommandItem = TryCast(e.Item, GridCommandItem)
            Dim txtFilter = TryCast(cmdItem.FindControl("txtFilter"), TextBox)
            For Each col As GridColumn In calendarTableView.MasterTableView.Columns
                For Each dataItem As GridDataItem In calendarTableView.Items
                    If dataItem(col.UniqueName).Text.Contains(txtFilter.Text) Then
                        filterExpression = "([" + col.UniqueName + "] = '%" + txtFilter.Text + "%')"
                        calendarTableView.MasterTableView.FilterExpression = filterExpression
                    End If
                Next
            Next
            calendarTableView.MasterTableView.Rebind()
        ElseIf e.CommandName = "ClearFilter" Then
            Dim cmdItem As GridCommandItem = TryCast(e.Item, GridCommandItem)
            Dim txtFilter As TextBox = TryCast(cmdItem.FindControl("txtFilter"), TextBox)
            txtFilter.Text = String.Empty
            calendarTableView.MasterTableView.FilterExpression = String.Empty
        End If
    End Sub

     

    but I am running into 2 problems.  1, it's not getting the text from the txtFilter textbox correctly and 2, the line " calendarTableView.MasterTableView.Rebind() " comes up with an exception saying it expected an expression.

  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5819 posts

    Posted 23 Nov 2018 Link to this post

    Hi,

    If the default grid filtering experience (see here and here) does not match your needs, you can also consider another built-in integration - with RadFilter: https://demos.telerik.com/aspnet-ajax/filter/examples/integration/defaultcs.aspx?product=filter.

    If you would still like to proceed with custom filtering, I advise that you consider filtering the data source itself rather than hacking through the grid. You can easily do that in the NeedDataSource event by processing the data according to the desired logic before passing it to the grid.

    Doing this manually is actually going to be rather hard and the provided code will have a lot of limitations. Nevertheless, I modified it so you can get started, and I also left comments with the issues and resources you can use to get things done:

    <telerik:RadGrid RenderMode="Lightweight" runat="server" ID="calendarTableView" OnItemCommand="ItemCommand" OnNeedDataSource="calendarTableView_NeedDataSource">
        <MasterTableView AutoGenerateColumns="false" TableLayout="Fixed" CommandItemDisplay="Top">
            <CommandItemTemplate>
                <asp:TextBox ID="txtFilter" runat="server"></asp:TextBox>
                <asp:Button ID="btnFilter" runat="server" Text="Apply Filter" CommandName="FilterAll" />
                <asp:Button ID="btnClear" runat="server" Text="Clear Filter" CommandName="ClearFilter" />
            </CommandItemTemplate>
            <Columns>
                <telerik:GridBoundColumn DataField="someField" HeaderText="some field"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="otherField" HeaderText="other field"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="thirdField" HeaderText="third field"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="fourthField" HeaderText="fourth field"></telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    Protected Sub ItemCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs)
        Dim filterExpression As String = String.Empty
        If e.CommandName = "FilterAll" Then
            Dim cmdItem As GridCommandItem = calendarTableView.MasterTableView.GetItems(GridItemType.CommandItem)(0)
            'TryCast(e.Item, GridCommandItem)
            Dim txtFilter = TryCast(cmdItem.FindControl("txtFilter"), TextBox)
            'these loops are unnecessary, it is similar to the operation that needs to be done on the data source for filtering anyway
            'also,they will only go through the data on the current page as the grid creates items only for the current page (for performance reasons)
            'which is why such custom filtering is best implemented over the data source rather than hacking through the grid
            'also, with this code the filter expression will be applied only for the last column where a text occurence exists
            For Each col As GridColumn In calendarTableView.MasterTableView.Columns
                For Each dataItem As GridDataItem In calendarTableView.Items
                    If dataItem(col.UniqueName).Text.Contains(txtFilter.Text) Then
                        'sample to aim for
                        '@"it[""CustomerID""].ToString().Contains(""ALFKI"")"
                        filterExpression = "it[""" + col.UniqueName + """].ToString().Contains(""" + txtFilter.Text + """)"
                        calendarTableView.MasterTableView.FilterExpression = filterExpression
                    End If
                Next
            Next
            If (Not String.IsNullOrEmpty(filterExpression)) Then
                'if you won't be doing operations on the grid, you do not really need to rebind it
                calendarTableView.MasterTableView.Rebind()
            End If
        ElseIf e.CommandName = "ClearFilter" Then
            Dim cmdItem As GridCommandItem = calendarTableView.MasterTableView.GetItems(GridItemType.CommandItem)(0)
            'Dim cmdItem As GridCommandItem = TryCast(e.Item, GridCommandItem)
            Dim txtFilter As TextBox = TryCast(cmdItem.FindControl("txtFilter"), TextBox)
            txtFilter.Text = String.Empty
     
     
            For Each col As GridColumn In calendarTableView.MasterTableView.Columns
                col.ListOfFilterValues = Nothing
                col.CurrentFilterFunction = GridKnownFunction.NoFilter
                col.CurrentFilterValue = String.Empty
            Next
     
            calendarTableView.MasterTableView.FilterExpression = String.Empty
        End If
    End Sub
     
     
    Protected Function GetTestData() As DataTable
        Dim tbl As New DataTable()
        tbl.Columns.Add(New DataColumn("someField", GetType(String)))
        tbl.Columns.Add(New DataColumn("otherField", GetType(String)))
        tbl.Columns.Add(New DataColumn("thirdField", GetType(Integer)))
        tbl.Columns.Add(New DataColumn("fourthField", GetType(String)))
        tbl.Rows.Add(New Object() {"first", "firstRecord2", 1, "A"})
        tbl.Rows.Add(New Object() {"first", "secondRecord2", 2, "B"})
        tbl.Rows.Add(New Object() {"third", "thirdRecord2", 3, "C"})
        Return tbl
    End Function
     
    Protected Sub calendarTableView_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs)
        DirectCast(sender, RadGrid).DataSource = GetTestData()
    End Sub


    Regards,
    Marin Bratanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top