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
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
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
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
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
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
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
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
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
Hi maria,
I apologize. I forgot that you refer me to the support ticket option. Please ignore the previous post.
Thanks,
Johnathan Beam
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:
- built-in filtering demo: http://demos.telerik.com/aspnet-ajax/grid/examples/functionality/filtering/basic-filtering/defaultcs.aspx.
- persisting grid settings out of the box: http://demos.telerik.com/aspnet-ajax/controls/examples/integration/persisting-grid-settings/defaultcs.aspx?product=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