Preserving ActiveSheet.RowFilter

Posted by: scottp on 25 June 2023, 5:20 pm EST

    • Post Options:
    • Link

    Posted 25 June 2023, 5:20 pm EST

    Hi,

    I have a situation where a user may apply row filtering to the grid. Programmatically, I want to be able to save any currently applied row filtering, clear the row filter, refresh the data, and then re-apply the preserved row filter.

    I am saving away the current row filter information like this:

    Dim myMemoryStream As New MemoryStream
    Dim myXmlTextWriter As New System.Xml.XmlTextWriter(myMemoryStream, System.Text.Encoding.UTF8)
    myXmlTextWriter.Formatting = Formatting.Indented
    myXmlTextWriter.Indentation = 2
    myXmlTextWriter.WriteStartDocument()
    myXmlTextWriter.WriteStartElement("AppliedFilter")
    activeSheet.RowFilter.Serialize(myXmlTextWriter)
    myXmlTextWriter.WriteEndElement()
    myXmlTextWriter.WriteEndDocument()
    myXmlTextWriter.Flush()
    If myMemoryStream.Position > 0 Then
    	myMemoryStream.Position = 0
    End If
    filterXmlDocument = New XmlDocument()
    filterXmlDocument.Load(myMemoryStream)
    myXmlTextWriter.Close()

    I am clearing the row filter like this:

    activeSheet.ClearRowFilter()

    I then set activeSheet.Rows.Count to 0, and repopulate the grid with saved data.

    Now I need to re-apply the saved RowFilter information. I am trying this:

    If filterXmlDocument IsNot Nothing Then
    	Dim node As System.Xml.XmlNode
    	node = filterXmlDocument.FirstChild
    	While Not node.Name.Equals("AppliedFilter")
    		node = node.NextSibling
    	End While
    	Dim myXMLNodeReader As New System.Xml.XmlNodeReader(node)
    	myXMLNodeReader.Read()
    	activeSheet.RowFilter.Deserialize(myXMLNodeReader)
    End If

    …but get an error of “System.NullReferenceException: ‘Object reference not set to an instance of an object.’ FarPoint.Win.Spread.SheetView.RowFilter.get returned Nothing.” at the “activeSheet.RowFilter.Deserialize(myXMLNodeReader)” line.

    Please advise. Should I even be using the Serialize() and Deserialize() methods for preserving and re-applying a row filter?

  • Posted 27 June 2023, 4:29 am EST

    Hi Scott,

    The error has occurred because SheetView’s ClearRowFilter() method set its RowFilter property to null and hence you got the error at “activeSheet.RowFilter.Deserialize(myXMLNodeReader)” line.

    We suggest you modify the code to apply saved filters as follows:

      fpSpread1.ActiveSheet.Columns(0, 5).AllowAutoFilter = True 'Filter property as set earlier
        fpSpread1.ActiveSheet.AutoFilterMode = FarPoint.Win.Spread.AutoFilterMode.EnhancedContextMenu  'The filter you set to your data previously
        If _filterXmlDocument IsNot Nothing Then
            Dim node = _filterXmlDocument.FirstChild
            While node IsNot Nothing AndAlso Not node.Name.Equals("AppliedFilter")
                node = node.NextSibling
            End While
            If node IsNot Nothing Then
                Dim myXMLNodeReader As XmlNodeReader = New XmlNodeReader(node)
                myXMLNodeReader.Read()
                fpSpread1.ActiveSheet.RowFilter.Deserialize(myXMLNodeReader)
            End If
        End If

    We have attached a sample application for your reference. Please check SaveRowFilter_VB.zip

    In case your requirement differs from our understanding, we request you provide us more information about how you repopulate the data as you have stated in the following line:

    “I then set activeSheet.Rows.Count to 0, and repopulate the grid with saved data.”

    It would be helpful if you could provide us with a stripped-down version of the sample application that shows the issue on your end. Alternatively you can update the attached sample so that we can replicate the issue on our end for further analysis.

    Thanks & Regards,

    Aastha

  • Posted 27 June 2023, 1:03 pm EST

    That’s fixed it. Thanks!

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels