FlexGrid for WinForms | ComponentOne
In This Topic
    Persistent State
    In This Topic

    Grid persistence refers to saving the current state of the grid for later use and then restoring it back when needed. For instance, the user may apply grouping, sorting, filtering or some styling to the grid which he wants to use after few more operations or on opening the application again. This behavior can be achieved by persisting the grid's state.

    There are two steps to implement persistence of the grid state. First, we have to serialize the grid state and then we restore it. In this topic, we discuss how to persist the filtered, sorted and grouped state of the FlexGrid control.

    You can implement persisting grid states, by using XMLWriter and XMLReader class of the System.XML namespace. To save the grid state, you can use the WriteStartElement method to write the specified start tag and WriteAttributeString method to write the filter, sort and group attributes. Finally, call WriteEndElement method to close the WriteStartElement method and save the state in the form of stream, string, or a file.

    Use the code below to serialize the state of WinForms FlexGrid to an XML file for later use.  

    //write the Filter,Group and Sort definitions to a file
    private void WriteStateToXML(string filePath) {
        using (XmlWriter writer = XmlWriter.Create(filePath))
        {
            writer.WriteStartElement("FlexGrid");
            writer.WriteStartElement("Definition", "");
            writer.WriteAttributeString("Filter", flexGrid.FilterDefinition);
            writer.WriteAttributeString("Sort", flexGrid.SortDefinition);
            writer.WriteAttributeString("Group", flexGrid.GroupDefinition);
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();
        }
    
    'write the Filter,Group and Sort definitions to a file
    Private Sub WriteStateToXML(ByVal filePath As String)
        Using writer = XmlWriter.Create(filePath)
            writer.WriteStartElement("FlexGrid")
            writer.WriteStartElement("Definition", "")
            writer.WriteAttributeString("Filter", flexGrid.FilterDefinition)
            writer.WriteAttributeString("Sort", flexGrid.SortDefinition)
            writer.WriteAttributeString("Group", flexGrid.GroupDefinition)
            writer.WriteEndElement()
            writer.WriteEndElement()
            writer.Flush()
        End Using
    End Sub
    

    To load the saved state back into the grid, you can use the GetAttribute method of the object to read the filter, sort and group attributes. Then, assign them to the FilterDefinition, SortDefinition or GroupDefinition properties of the FlexGrid respectively.

    Use the code below to load saved state of the WinForms FlexGrid:

    //Read the Filter, Sort and Group definitions from the saved file and apply it to the flexGrid
    private void ReadStateXML(string filePath) {
    
        using (XmlReader reader = XmlReader.Create(filePath)) {
            reader.ReadToFollowing("Definition");
            flexGrid.SortDefinition = reader.GetAttribute("Sort");
            flexGrid.FilterDefinition = reader.GetAttribute("Filter");
            flexGrid.GroupDefinition = reader.GetAttribute("Group");
            reader.Close();
        }
    
    'Read the Filter, Sort and Group definitions from the saved file and apply it to the flexGrid
    Private Sub ReadStateXML(ByVal filePath As String)
        Using reader = XmlReader.Create(filePath)
            reader.ReadToFollowing("Definition")
            flexGrid.SortDefinition = reader.GetAttribute("Sort")
            flexGrid.FilterDefinition = reader.GetAttribute("Filter")
            flexGrid.GroupDefinition = reader.GetAttribute("Group")
            reader.Close()
        End Using