How to add filter for only one column in rad grid ?

2 Answers 35 Views
GridView
Aravind
Top achievements
Rank 2
Iron
Iron
Iron
Aravind asked on 03 Jul 2024, 04:42 AM | edited on 03 Jul 2024, 08:56 AM
Hi,
    I want to add filter for only one column with the value of "Verification" , "Export" and "Done". so how to add this filter in rad grid. Can add dropdownlist in order show 3 values and filter by any one value chosen by end user 

Regards
Aravind
Aravind
Top achievements
Rank 2
Iron
Iron
Iron
commented on 03 Jul 2024, 09:01 AM

I tried following code , but error in code.
Imports Telerik.WinControls.Data
Imports Telerik.WinControls.UI

Public Class Form4
    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles Me.Load
        ' Initialize RadGridView
        Dim radGridView As New RadGridView()
        radGridView.Dock = DockStyle.Fill
        Me.Controls.Add(radGridView)

        ' Set up some sample data
        Dim dataTable As New DataTable()
        dataTable.Columns.Add("ID", GetType(Integer))
        dataTable.Columns.Add("Name", GetType(String))
        dataTable.Columns.Add("Age", GetType(Integer))
        dataTable.Columns.Add("Gender", GetType(String))

        dataTable.Rows.Add(1, "John Doe", 30, "Male")
        dataTable.Rows.Add(2, "Jane Smith", 25, "Female")
        dataTable.Rows.Add(3, "Samuel Adams", 40, "Male")
        dataTable.Rows.Add(4, "Sara Connor", 35, "Female")

        ' Set the data source
        radGridView.DataSource = dataTable

        ' Enable filtering
        radGridView.EnableFiltering = True

        ' Customize filtering with dropdown list
        AddHandler radGridView.CreateCellElement, AddressOf radGridView_CreateCellElement
        AddHandler radGridView.CellEditorInitialized, AddressOf radGridView_CellEditorInitialized


    End Sub
    Private Sub radGridView_CreateCellElement(sender As Object, e As GridViewCreateCellEventArgs)
        If e.CellType Is GetType(GridFilterCellElement) AndAlso e.Column.Name = "Gender" Then
            e.CellElement = New CustomFilterCellElement(e.Column, e.Row)
        End If
    End Sub

    Private Sub radGridView_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs)
        If TypeOf e.ActiveEditor Is RadDropDownListEditor Then
            Dim editor As RadDropDownListEditor = CType(e.ActiveEditor, RadDropDownListEditor)
            Dim element As RadDropDownListEditorElement = CType(editor.EditorElement, RadDropDownListEditorElement)
            If e.Column.Name = "Gender" Then
                element.Items.Clear()
                element.Items.Add("All")
                element.Items.Add("Male")
                element.Items.Add("Female")
                AddHandler element.SelectedIndexChanged, AddressOf DropDownList_SelectedIndexChanged
            End If
        End If
    End Sub

    Private Sub DropDownList_SelectedIndexChanged(sender As Object, e As Telerik.WinControls.UI.Data.PositionChangedEventArgs)
        Dim editorElement As RadDropDownListEditorElement = DirectCast(sender, RadDropDownListEditorElement)
        Dim selectedValue As String = editorElement.SelectedItem.Text
        Dim gridView As RadGridView = DirectCast(editorElement.GridControl, RadGridView)

        gridView.MasterTemplate.FilterDescriptors.Clear()
        If selectedValue <> "All" Then
            Dim filterDescriptor As New FilterDescriptor("Gender", FilterOperator.IsEqualTo, selectedValue)
            gridView.MasterTemplate.FilterDescriptors.Add(filterDescriptor)
        End If
    End Sub

    Public Class CustomFilterCellElement
        Inherits GridFilterCellElement

        Public Sub New(column As GridViewColumn, row As GridRowElement)
            MyBase.New(column, row)
        End Sub

        Protected Overrides Function CreateEditor() As RadDropDownListEditor
            Return New RadDropDownListEditor()
        End Function

        Protected Overrides Sub CreateChildElements()
            MyBase.CreateChildElements()
        End Sub

        Public Overrides Function IsCompatible(data As GridViewColumn, context As Object) As Boolean
            Return TypeOf data Is GridViewDataColumn AndAlso data.Name = "Gender"
        End Function
    End Class
End Class

2 Answers, 1 is accepted

Sort by
0
Accepted
Aravind
Top achievements
Rank 2
Iron
Iron
Iron
answered on 17 Jul 2024, 10:39 AM

Hi,  i am not sure this is correct way or not, i did following way to filter the grid from combobox SelectedIndexChanged.

I already added columns in radgrid with row values too  while page load and from the ComboBox1_SelectedIndexChanged event i added following code to filter.

   Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim valuess As String = ComboBox1.SelectedItem

        If valuess = "ALL" Then
            RadGridView1.FilterDescriptors.Clear()
        Else
            RadGridView1.FilterDescriptors.Clear()
            Dim filter As New FilterDescriptor()
            filter.PropertyName = "NextStage"
            filter.[Operator] = FilterOperator.IsEqualTo
            filter.Value = ComboBox1.SelectedItem
            filter.IsFilterEditor = True
            Me.RadGridView1.FilterDescriptors.Add(filter)
        End If
    End Sub

"NextStage" is one of the column name in grid. so "ALL" means show all values if any one of these "Verification" , "Export" and "Done" it filter in grid. Filteroperator i set isequalto, so it show exact value select in combobox selected value.


Note: In grid not show any filter icons (default filter icon)

Thanks and Regards
Aravind

Nadya | Tech Support Engineer
Telerik team
commented on 22 Jul 2024, 06:06 AM

Hello, Aravind,

The provided code snippet for adding a simple FilterDescriptor seems ok to me. However, without having the exact setup that you have I cannot determine why the filter icon is not visible on your end. This icon remains visible in cells when filtering functionality is enabled (EnableFiltering = true;), but it seems grayed out when there is no filter applied. Once, a filter for the column is applied the filter icon becomes in active state and usually appears in black color. Can you please specify whether the filter icon is not visible at all or only when filtering data and filter is applied?

Also, is the data filtered correctly on your side after applying the filter? Is it possible for you to attach a simplified project that shows the problem you had with applying filters together with the implementation about the custom CustomFilterCellElement that you have. Thus, we could be able to understand better the exact case that you have and assist you more precisely with suitable solution. Thank you in advance for your cooperation.

I am looking forward to your reply.

0
Dinko | Tech Support Engineer
Telerik team
answered on 05 Jul 2024, 09:57 AM

Hi Aravind,

If you want to filter multiple columns at the same time, you can use CompositeFilterDescriptor. More information can be found in the Setting Filters Programmatically (simple descriptors) help article.  

Upon checking the code I am assuming that the error appears in the custom filter cell as CreateEditor() method does not exist. In this case, you can use permanent drop-down editor and use it to filter the column. Such an approach is demonstrated in the Permanent editor in a filter cell KB article in our documentation.

I hope that the above shared resources will be helpful.

Regards,
Dinko | Tech Support Engineer
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.

Aravind
Top achievements
Rank 2
Iron
Iron
Iron
commented on 12 Jul 2024, 09:56 AM | edited

Hi,
  Is it possible to filter from outside grid, i mean with dropdown and button outside the grid, so based on dropdown value can filter in grid ?

Note: Filter in grid, not reload data when filter. Bcz values are bind form system folder (file name bind in grid view) , not from sql. This project not have sql server.

Regards
Aravind
Dinko | Tech Support Engineer
Telerik team
commented on 15 Jul 2024, 06:54 AM

Using the approach demonstrated in the Setting Filters Programmatically (composite descriptors) help article you can filter the control programmatically. With this in hand, you could create separate controls that the end user can select, and depending on its selection, you can create CompositeFilterDescriptor in code and apply it to the RadGridView. 

Our FilterView control is using a similar approach. When the RadFilterView control is associated with the RadGridView, it will create the filter section for each column, and depending on the user preferences, the control internally creates filter descriptors and applies them to the control. You could consider using RadFilterView control if it is more close to your requirement. To get familiar with the control, you can start with the following articles.

Let me know if further assistance is required.

Tags
GridView
Asked by
Aravind
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Aravind
Top achievements
Rank 2
Iron
Iron
Iron
Dinko | Tech Support Engineer
Telerik team
Share this question
or