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

Remove/disable a column filter for a removed column

10 Answers 158 Views
Filter
This is a migrated thread and some comments may be shown as answers.
Johnathan
Top achievements
Rank 1
Johnathan asked on 16 Oct 2015, 04:14 PM

Hello all,

 I have a page created that allows our users to customize what data columns are available to them and also use a radfilter to apply & save filters for these columns. A common occurrence for our users is they remove a column & forget to remove the filter for that column, so when filter is loaded through the LoadSettings method, an error is produced. Is there a way for me to remove a column filter for the removed column after the LoadSettings is called but before the filter is applied?

10 Answers, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 21 Oct 2015, 08:00 AM
Hello Johnathan,

In order to achieve this you would have to find the filter expression with a FieldName matching that of the column. This can be achieved by using this code snippet:
for (int i = 0; i < RadFilter1.RootGroup.Expressions.Count; i++)
        {
            string filteredField = (RadFilter1.RootGroup.Expressions[i] as RadFilterNonGroupExpression).FieldName;
            int filterExpressionIndex = i;
        }

Later you can remove the expression by index:
//remove the filter expression
        RadFilter1.RootGroup.Expressions.Remove(RadFilter1.RootGroup.Expressions[filterExpressionIndex ]);
        RadFilter1.RecreateControl();

I hope this helps.

Regards,
Maria Ilieva
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Johnathan
Top achievements
Rank 1
answered on 21 Oct 2015, 01:51 PM

Hi Maria,

 I appreciate the reply, but I'm getting an "Object reference not set to an instance of an object" error when I get to the Radfilter's RecreateControl call. Any ideas on what I'm doing wrong?

Thanks,

 Johnathan

Try
                            RadFilter1.LoadSettings(oData("FilterValue"))
 
                        Catch ex As Exception
 
                        End Try
 
                        Dim sFilterIndexes As String = ""
 
                        For i As Integer = 0 To RadFilter1.RootGroup.Expressions.Count - 1
                            Dim bExists As Boolean = False
                            Dim sAvailableColumnSplit As String() = sAvailableColumns.Split(";")
 
                            For k As Integer = 0 To sAvailableColumnSplit.Length - 1
                                If sAvailableColumnSplit(k) = TryCast(RadFilter1.RootGroup.Expressions(i), RadFilterNonGroupExpression).FieldName Then
                                    bExists = True
                                End If
                            Next
 
                            If Not (bExists) Then
                                sFilterIndexes = i.ToString & ";"
                            End If
                        Next
 
                        If sFilterIndexes <> "" Then
                            Dim sFilterIndexSplit As String() = sFilterIndexes.Split(";")
 
                            For i As Integer = 0 To sFilterIndexSplit.Length - 1
                                If sFilterIndexSplit(i) <> "" Then
                                    RadFilter1.RootGroup.Expressions.Remove(RadFilter1.RootGroup.Expressions(i))
                                    RadFilter1.RecreateControl()
                                End If
                            Next

0
Maria Ilieva
Telerik team
answered on 26 Oct 2015, 08:55 AM
Hi Johnathan,

Can you please share your fill page markup as well as related code behind so that i can revise the code locally and advise you further?

Regards,
Maria Ilieva
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Johnathan
Top achievements
Rank 1
answered on 09 Jun 2016, 12:20 PM

Hi Maria,

I apologize for the long reply. Here's the code I use to fill the radgrid:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim oConn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("Conn1").ConnectionString)
        Dim oConn2 As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("Conn2").ConnectionString)
        Dim oCmd As New SqlCommand
        Dim oData As New SqlDataAdapter
        Dim iValue As Integer = 0
        Dim bFlagUpdate As Boolean = False
        Try
            If iWebID > 0 Then
                'AllTasksToggle.SelectedValue = tSelectedAllTaskValue.Text
 
                oConn.Open()
                oConn2.Open()
 
                If CInt(FlagID.Text) > 0 And CInt(TaskID.Text) > 0 Then
                    'the user wants to flag a task
                    With oCmd
                        .Parameters.Clear()
                        .Connection = oConn2
                        .CommandType = CommandType.StoredProcedure
                        .CommandText = "Flag_SP"
                        .Parameters.AddWithValue("@UserID", iWebID)
                        .Parameters.AddWithValue("@FlagID", FlagID.Text)
                        .Parameters.AddWithValue("@TaskID", TaskID.Text)
                        .ExecuteNonQuery()
                    End With
 
                    FlagID.Text = 0
                    TaskID.Text = 0
 
                    bFlagUpdate = True
                End If
 
                With oCmd
                    .Parameters.Clear()
                    .Connection = oConn2
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "Data_Pull_SP"
                    .Parameters.AddWithValue("UserID", iWebID)
                    .Parameters.AddWithValue("TaskTypeID", iTaskTypeID)
                    .Parameters.AddWithValue("AllTasks", IIf(AllTasksToggle.SelectedValue = "", 0, AllTasksToggle.SelectedValue))
                End With
 
                oData.SelectCommand = oCmd
 
                Dim dt As New DataTable
 
                oData.Fill(dt)
 
                If dt.Columns.Count = 0 And iWebID > 0 Then
                    Response.Redirect("ManageViewPreferences.aspx?Tab=" & sTaskType, False)
                    Exit Sub
                Else
                    For Each col As DataColumn In dt.Columns
                        sAvailableColumns &= col.ColumnName & ";"
                    Next
                End If
 
                RadGrid1.DataSource = dt
 
                'Now, load the filter settings if a default filter is available & if not saving/deleting a filter
                bDelete.Enabled = IIf(rcFilters.SelectedValue = 0, False, True)
                If rcFilters.SelectedValue <> 0 And rcFilters.SelectedValue <> tPreviousFilter.Text And tDisableSettingsLoad.Text = "False" Then
                    tPreviousFilter.Text = rcFilters.SelectedValue
                    RadFilter1.RootGroup.Expressions.Clear()
                    RadFilter1.RecreateControl()
                    LoadFilterSettings(rcFilters.SelectedValue)
                ElseIf rcFilters.SelectedValue = 0 And rcFilters.SelectedValue <> tPreviousFilter.Text Then
                    tPreviousFilter.Text = rcFilters.SelectedValue
                    RadFilter1.RootGroup.Expressions.Clear()
                    RadFilter1.RecreateControl()
                    RadFilter1.FireApplyCommand()
                    RadGrid1.Rebind()
                ElseIf tSelectedAllTaskValue.Text = "" Or tSelectedAllTaskValue.Text <> AllTasksToggle.SelectedValue Then
                    tSelectedAllTaskValue.Text = AllTasksToggle.SelectedValue
                    If rcFilters.SelectedValue = 0 Then
                        RadFilter1.RootGroup.Expressions.Clear()
                        RadFilter1.RecreateControl()
                        RadFilter1.FireApplyCommand()
                        RadGrid1.Rebind()
                    Else
                        tPreviousFilter.Text = rcFilters.SelectedValue
                        RadFilter1.RootGroup.Expressions.Clear()
                        RadFilter1.RecreateControl()
                        LoadFilterSettings(rcFilters.SelectedValue)
                        If bFlagUpdate Then
                            RadGrid1.Rebind()
                        End If
                    End If
                ElseIf bFlagUpdate Then
                    RadGrid1.Rebind()
                End If
 
                'Create filter hyperlink to be display
                LinkSpan.InnerHtml = "<a href=" & Chr(34) & "CustomView.aspx?FilterID=" & rcFilters.SelectedValue & Chr(34) & " target=" & Chr(34) & "_blank" & Chr(34) & ">Link to selected filter</a>"
            End If
 
            If ExportType.Text <> "" Then
                Call bExportTasksGrid_Click(sender, e)
                ExportType.Text = ""
            End If
 
            If Reload.Text = "True" Then
                Reload.Text = "False"
                Call SaveFilter(sender, e)
            End If
 
        Catch ex As Exception
        Finally
            oConn.Dispose()
            oConn2.Dispose()
            oConn = Nothing
            oConn2 = Nothing
            oCmd = Nothing
            oData = Nothing
        End Try
    End Sub

0
Maria Ilieva
Telerik team
answered on 14 Jun 2016, 11:45 AM
Hi Johnathan,

I revise the provided code and noticed that you are using simple DataBinding for the RadGrid control. Note that the required functionality is only supported with advanced DataBinding. See the topic below for more information on that matter:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-binding/understanding-data-binding/server-side-binding/advanced-data-binding-(using-needdatasource-event)

Regards,
Maria Ilieva
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Johnathan
Top achievements
Rank 1
answered on 14 Jun 2016, 01:54 PM

Hi Maria,

I have moved the grid data pull to the NeedDataSource event, but I'm still getting an "Object reference not set to an instance of an object" error when I get to the Radfilter's RecreateControl call. Any additional help would be greatly appreciated.

Thanks,

Johnathan

0
Maria Ilieva
Telerik team
answered on 17 Jun 2016, 12:22 PM
Hello Johnathan,

In this case it will be best if you can open a regular support ticket and send us sample runnbale version of your application so that we can debug it locally and advise you further.

Regards,
Maria Ilieva
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Johnathan
Top achievements
Rank 1
answered on 09 May 2017, 03:24 PM

Hi Maria,

Sorry for the long reply. I'm finally getting back to looking into this problem. I've moved the radgrid data pull to the needdatasource event, but I'm still receiving the same error when running the radfilter check. Currently, I trigger the needdatasource event by using radgrid1.databind() then try loading the filter settings & running the check. This happens in the page load event. Please let me know if there's something I'm missing.

Thanks,

Johnathan Beam

0
Johnathan
Top achievements
Rank 1
answered on 09 May 2017, 03:27 PM

Hi maria,

I apologize. I forgot that you refer me to the support ticket option. Please ignore the previous post.

Thanks,

Johnathan Beam

0
Marin Bratanov
Telerik team
answered on 11 May 2017, 10:02 AM

Hi Johnathan,

I am pasting here my reply to the support ticket in case anyone else has a similar scenario:

The first suggestion I can make is to drop the filter and use the built-in filtering and persistence of the grid:

This will let your users filter the grid, toggle columns, filter, group and so on, and you can save/restore that state easily, without additional controls and code.

The other thing I can suggest is removing call to RadGrid1.DataBind() from Page_Load because it must not be invoked when using the NeedDataSource event.

 

 

Regards,

 

Marin Bratanov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Filter
Asked by
Johnathan
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Johnathan
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or