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

EnableRangeFiltering InvalidCastException

8 Answers 112 Views
Filter
This is a migrated thread and some comments may be shown as answers.
Kyle
Top achievements
Rank 1
Kyle asked on 25 Oct 2012, 08:42 PM
I am having trouble with range filtering. All other filtering on my radgrid works fine, but range filtering is not returning the correct results, it appears to just be reloading the radgrid.  I have tried Operating with the FilterExpression of Telerik RadGrid Manually as seen in the article in hopes that I would be able to see the values for the "From" and "To" date boxes, but I keep getting an "InvalidCastException" when the breakpoint reaches "Dim startDate As TextBox" in both examples. I can not seem to find code that allows be to access those text boxes. Thanks in advance for you help!

        If e.CommandName = RadGrid.FilterCommandName Then
            Dim filterPair As Pair = TryCast(e.CommandArgument, Pair)
            Dim filterName As String = Convert.ToString(filterPair.First)
            Dim columnName As String = Convert.ToString(filterPair.Second)
            If columnName = "PUBLISHED_DATE" Then
                e.Canceled = True
  
                Dim startDate As TextBox = CType((CType(e.Item, GridFilteringItem))(filterPair.Second.ToString()).Controls(0), TextBox)
                Dim endDate As TextBox = TryCast(TryCast(e.Item, GridFilteringItem)(filterPair.Second.ToString()).Controls(1), TextBox)
-------------------------------------------------------------------------------------------
  
AND
  
  
        If e.CommandName = RadGrid.FilterCommandName Then
            Dim filterPair As Pair = TryCast(e.CommandArgument, Pair)
            Dim filterName As String = Convert.ToString(filterPair.First)
            Dim columnName As String = Convert.ToString(filterPair.Second)
            If columnName = "PUBLISHED_DATE" Then
                e.Canceled = True
                Dim filteringItem As GridFilteringItem = TryCast(e.Item, GridFilteringItem)
  
                Dim startDate As TextBox = DirectCast(filteringItem("PUBLISHED_DATE").Controls(0), TextBox)
                Dim endDate As TextBox = DirectCast(DirectCast(e.Item, GridFilteringItem)(filterPair.Second.ToString()).Controls(1), TextBox)

8 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 26 Oct 2012, 05:19 AM
Hi Kyle,

The CommandArgument Pair should contain date-time objects and you should be casting to those. Certainly, the pair does not contain text boxes.


All the best,
Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Kyle
Top achievements
Rank 1
answered on 26 Oct 2012, 01:20 PM
Im not sure I follow what you are saying?

"The CommandArgument Pair should contain date-time objects"  When I access the CommandArgument Pair it contains "filter type" and column "unique name".

Dim filterName As String = Convert.ToString(filterPair.First)
Dim columnName As String = Convert.ToString(filterPair.Second)

That code gives me "filterName = Between" & "columnName = Published_Date"

Could you please elaborate on your original response?
0
Tsvetoslav
Telerik team
answered on 31 Oct 2012, 08:09 AM
Hi Kyle,

Sorry for that - my original response was based on a misunderstanding of your code. Could you paste your complete code-behind so that I can go through it and see what's going on.

Greetings, Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Kyle
Top achievements
Rank 1
answered on 31 Oct 2012, 01:37 PM

As I said in my original post, I am attempting to introduce basic filtering into my radgrid. The basic filtering works with one exception, I cannot get range filtering to function. I can input a "From:" and "To:" date and choose my filter type of "Between" and it will rebind the grid with a subset of results that were contained in the original radgrid. But none of the returned results match my date range search and I cannot determine what the results have in common and why they were returned.

My next step was to determine if there was an error with the grid or a problem with my data. I found this article "Operating with the FilterExpression of Telerik RadGrid Manually" which would allow me to trouble shoot a problem with the grid by allowing me to place a breakpoint on the "ItemCommand" and see what dates the radgrid was working with. Example from article:

Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.ItemCommand
        If e.CommandName = RadGrid.FilterCommandName Then
            Dim filterPair As Pair = DirectCast(e.CommandArgument, Pair)
            gridMessage1 = "Current Filter function: '" + filterPair.First + "' for column '" + filterPair.Second + "'"
            Dim filterBox As TextBox = CType((CType(e.Item, GridFilteringItem))(filterPair.Second.ToString()).Controls(0), TextBox)
            gridMessage2 = "<br> Entered pattern for search: " + filterBox.Text
        End If
    End Sub
    Private gridMessage1 As String = Nothing
    Private gridMessage2 As String = Nothing
    Protected Sub RadGrid1_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles RadGrid1.DataBound
        If Not String.IsNullOrEmpty(gridMessage1) Then
            DisplayMessage(gridMessage1)
            DisplayMessage(gridMessage2)
        End If
    End Sub
    Private Sub DisplayMessage(ByVal text As String)
        RadGrid1.Controls.Add(New LiteralControl(String.Format("<span style='color:red'>{0}</span>", text)))
    End Sub

I took the above example and began to modify it to suit my code. I have yet to find a valid way to Cast the information from the "From:" & "To:" date range boxes so that I may begin to trouble shout my issue with range filtering.

If e.CommandName = RadGrid.FilterCommandName Then
    Dim filterPair As Pair = TryCast(e.CommandArgument, Pair)
    Dim filterName As String = Convert.ToString(filterPair.First)
    Dim columnName As String = Convert.ToString(filterPair.Second)
    If columnName = "PUBLISHED_DATE" Then
        e.Canceled = True
        Dim filteringItem As GridFilteringItem = TryCast(e.Item, GridFilteringItem)
        Dim startDate As TextBox = DirectCast(filteringItem("PUBLISHED_DATE").Controls(0), TextBox)
        Dim endDate As TextBox = DirectCast(DirectCast(e.Item, GridFilteringItem)(filterPair.Second.ToString()).Controls(1), TextBox)
        Dim sDate As String = startDate.Text
        Dim eDate As String = endDate.Text
        Dim newFilter As String = "('" & startDate.ToString("MM/dd/yyyy") & "' <= [PUBLISHED_DATE] AND [PUBLISHED_DATE] <= '" & endDate.ToString("MM/dd/yyyy") & "')"
        Dim dateColumn As GridBoundColumn = DirectCast(e.Item.OwnerTableView.GetColumnSafe(columnName), GridBoundColumn)
        dateColumn.CurrentFilterValue = startDate.ToString("MM/dd/yyyy")
        gridSearchResults.MasterTableView.FilterExpression = newFilter
        gridSearchResults.Rebind()
    End If
End If

0
Kyle
Top achievements
Rank 1
answered on 02 Nov 2012, 03:26 PM
This is the code behind from my test page, it is parsed code from my main page

Imports System.Data
Imports System.Data.OracleClient
Imports Telerik.Web.UI
Imports SecureVal
  
Partial Class Test_Search
    Inherits System.Web.UI.Page
  
#Region "Properties"
  
    Dim SQLConnStr As String = ConfigurationManager.ConnectionStrings("DDConnStr").ConnectionString
#End Region
  
#Region "Telerik"
  
    Private Function GetRGvSearchResults(ByVal srcType As String, ByVal srcTerm As String) As Object
        Dim SQL As String = ""
  
        SQL = "select distinct ID, CASE TYPE when'RULE' then DESCRIPTION when 'RULESTATEMENT' then DESCRIPTION else NAME end as NAME, TYPE, DESCRIPTION, VERSION, STANDARDIZATION_STATUS, RULE_PUB, C2IDDSTANDARD_PUB, OTHER_PUB, PUB_TYPE, to_char(to_date(PUBLISHED_DATE),'MM/DD/YYYY') as PUBLISHED_DATE FROM VW_ALL_OBJECT_TYPES WHERE TYPE = '" & srcType & "' ORDER by TYPE, Ltrim(Upper(NAME)), VERSION, PUBLISHED_DATE"
  
        Select Case UCase(srcType)
            Case "PUBLICATION"
                labTopSearchTitle.Text = "Publications"
                txtSearchType.Value = SecStr(srcType)
                labPageDesc.Text = "Below are registered publications."
                gridSearchResults.Columns(2).HeaderStyle.Width = 150
                gridSearchResults.Columns(3).Visible = True
                gridSearchResults.Columns(3).HeaderStyle.Width = 75
                gridSearchResults.Columns(4).HeaderStyle.Width = 275
                gridSearchResults.Columns(5).Visible = True
                gridSearchResults.Columns(5).HeaderStyle.Width = 350
                gridSearchResults.Columns(6).Visible = True
                gridSearchResults.Columns(6).HeaderStyle.Width = 65
                gridSearchResults.Columns(7).Visible = True
                gridSearchResults.Columns(8).Visible = True
                gridSearchResults.Columns(9).Visible = True
                gridSearchResults.Columns(10).Visible = True
        End Select
  
        Dim EISRSQL As String = SQL
        Dim table As New DataTable()
        Dim EISRComm As New OracleCommand
        Dim EISRConn As New OracleConnection(SQLConnStr)
        Dim adapter As OracleDataAdapter = New OracleDataAdapter
        adapter.SelectCommand = New OracleCommand(SecSQL(EISRSQL), EISRConn)
        EISRConn.Open()
        Try
            adapter.Fill(table)
            gridSearchResults.DataSource = table
            If table.Rows.Count = 0 Then
                gridSearchResults.DataSource = Nothing
                labDebug.Text = "No results for " & SecStr(srcTerm)
             End If
        Catch ex As Exception
            If ConfigurationManager.AppSettings("ErrorMode").ToString = "DEVL" Then
                labDebug.Visible = True
                labDebug.Text = labDebug.Text & "There was a problems:<br/>" & ex.Message.ToString & "<br />" ' & SQL
            Else
                Response.Redirect("~/errors.aspx")
            End If
        Finally
            EISRConn.Close()
            EISRConn.Dispose()
            OracleConnection.ClearPool(EISRConn)
        End Try
        Return table
    End Function
  
    Protected Sub gridSearchResults_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles gridSearchResults.NeedDataSource
        GetRGvSearchResults("PUBLICATION", "")
    End Sub
  
    Protected Sub gridSearchResults_SortCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridSortCommandEventArgs) Handles gridSearchResults.SortCommand
        gridSearchResults.DataSource = Nothing
        gridSearchResults.Rebind()
    End Sub
  
    Protected Sub gridSearchResults_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gridSearchResults.ItemCreated
        '------ this is the styling for each cell inside RADgrid -----
        If e.Item.ItemType = Telerik.Web.UI.GridItemType.Header Then
            For Each cell As TableCell In e.Item.Cells
                cell.Style("font-weight") = "normal"
                cell.Style("vertical-align") = "top"
            Next
        End If
    End Sub
#End Region
  
#Region "Init"
  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            GetRGvSearchResults("PUBLICATION", "")
        End If
    End Sub
  
    Protected Sub gridSearchResults_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles gridSearchResults.ItemCommand
  
    End Sub
#End Region
  
#Region "Functions"
  
    Function FormatTypePage(ByVal strType As Object, ByVal strID As Object) As String
        Dim retVal As String = ""
        Dim thisLocation As String = ""
        If Not SecStr(Request.QueryString("t")) = "" Then
            thisLocation = SecStr(Request.QueryString("t"))
        Else
            thisLocation = SecStr(Request.QueryString("s"))
        End If
  
        If Not IsDBNull(strType) Then
            Select Case strType
                Case "PUBLICATION"
                    retVal = "PubDetail.aspx?o=" & strID.ToString & "&l=" & thisLocation
            End Select
        End If
  
        Return retVal
    End Function
  
    Function FormatData(ByVal showStr As String) As String
        Dim retStr As String = String.Empty
        Select Case showStr
            Case "Y"
                retStr = "<b style='color:green; font-family: Wingdings 2;'>P</b>"
            Case Else
                retStr = String.Empty
        End Select
        Return retStr
    End Function
  
#End Region
End Class
0
Tsvetoslav
Telerik team
answered on 05 Nov 2012, 01:33 PM
Hello Kyle,

You should be casting to RadDatePicker type, not TextBox. Do use the Visual Studio debugger to inspect what type of controls you have elsewhere you need to cast them to the correct type.

Regards, Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Kyle
Top achievements
Rank 1
answered on 05 Nov 2012, 03:37 PM
So I have made the change from textbox to RadDatePicker as you suggested and still no luck. All 3 attempts listed below produce the same issue. "Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'Telerik.Web.UI.RadDatePicker'." Please advise 

Dim startDate As RadDatePicker = CType((CType(e.Item, GridFilteringItem))(filterPair.Second.ToString()).Controls(0), RadDatePicker)
 
Dim startDate As RadDatePicker = DirectCast(DirectCast(e.Item, GridFilteringItem)(filterPair.Second.ToString()).Controls(0), RadDatePicker)
 
Dim startDate As RadDatePicker = DirectCast(filteringItem("PUBLISHED_DATE").Controls(0), RadDatePicker)
0
Tsvetoslav
Telerik team
answered on 06 Nov 2012, 03:14 PM
Hello Kyle,

As I have already mentioined, do debug your code in order to get a clear picture on what controls are in your code signatures. You should be getting the second control in the fitler item cell, not the first one. The first one is a literal:
Dim startDate As RadDatePicker = CType((CType(e.Item, GridFilteringItem))(filterPair.Second.ToString()).Controls(1), RadDatePicker)
  
  
  
Dim startDate As RadDatePicker = DirectCast(DirectCast(e.Item, GridFilteringItem)(filterPair.Second.ToString()).Controls(1), RadDatePicker)
  
  
  
Dim startDate As RadDatePicker = DirectCast(filteringItem("PUBLISHED_DATE").Controls(1), RadDatePicker)


Greetings, Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Filter
Asked by
Kyle
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Kyle
Top achievements
Rank 1
Share this question
or