Skip to main content Skip to footer

Save State with C1FlexGrid for Silverlight

We can easily save the current state of the C1DataGrid and Greg's post here discusses the implementation in detail. In this post, we will see how we can save the state of C1FlexGrid. The approach is quite similar with some minor tweaking. C1FlexGrid already provides ColumnLayout property which can be used to get/set the current layout of the columns. Apart from this, the C1FlexGridFilter exposes the FilterDefnition property which one can use for saving the current filter state. Saving/loading the group/sort state is what we need to handle manually and then combine together to save the flexgrid's entire state. Below is the simple class we would be using to save the sort/group state:

public class SortedColumns  
{  
    public string ColumnName { get; set; }  
    public ListSortDirection Direction { get; set; }  
}  

public class ExtendedSettings  
{  
    public  List<SortedColumns> Sorts{ get; set; }  
    public List<string> Groups { get; set; }  
}

Next is the class which combines all the required 'settings' together:

public class FlexGridSettings  
{  
    public FlexGridSettings()  
    {  
       this.SortsGroups = new ExtendedSettings();  
       this.SortsGroups.Sorts = new List<SortedColumns>();  
       this.SortsGroups.Groups = new List<string>();  
    }  
    public ExtendedSettings SortsGroups { get; set; }  
    public string FilterDefinition { get; set; }  
    public string ColumnLayout { get; set; }  
}

Lets see how we save the settings to IsolatedStorage using the above classes. The below function simply saves the current state:

public void SaveState()  
{  
    var settings = new FlexGridSettings();  
    settings.ColumnLayout = _flex.ColumnLayout;  
    settings.FilterDefinition = C1FlexGridFilterService.GetFlexGridFilter(_flex).FilterDefinition;  
    foreach (var sorts in _flex.CollectionView.SortDescriptions)  
    {  
        settings.SortsGroups.Sorts.Add(new SortedColumns() { ColumnName = sorts.PropertyName, Direction = sorts.Direction });  
    }  
    foreach (var groups in _flex.CollectionView.GroupDescriptions)  
    {  
        settings.SortsGroups.Groups.Add((groups as PropertyGroupDescription).PropertyName);  
    }  

    var layoutFileName = "ColumnLayout";  
    IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();  
    using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(layoutFileName, FileMode.Create, isoFile))  
    {  
        XmlSerializer ser = CreateSerializer();  
        ser.Serialize(isoStream, settings);  
    }  
}

The below code loads the state from the file in isolatedstorage:

private FlexGridSettings GetFlexGridSettings()  
{  
    var layoutFileName = "ColumnLayout";  
    IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();  
    if ( isoFile.FileExists(layoutFileName))  
    {  
       using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(layoutFileName, FileMode.Open, isoFile))  
       {  
            try  
            {  
                 XmlSerializer ser = CreateSerializer();  
                 return (FlexGridSettings)ser.Deserialize(isoStream);  
            }  
            catch  
           {  
           }  
       }  
    }  
    return new FlexGridSettings();  
}  

public void LoadState()  
{  
     var fgs = GetFlexGridSettings();  
     if (fgs != null)  
     {  
         _flex.ColumnLayout = fgs.ColumnLayout;  
         C1FlexGridFilterService.GetFlexGridFilter(_flex).FilterDefinition = fgs.FilterDefinition;  
         foreach (var sorts in fgs.SortsGroups.Sorts)  
         {  
              _flex.CollectionView.SortDescriptions.Add(new SortDescription(sorts.ColumnName, sorts.Direction));  
         }  
         foreach (var groups in fgs.SortsGroups.Groups)  
         {  
              _flex.CollectionView.GroupDescriptions.Add(new PropertyGroupDescription(groups));  
         }  
     }  
}

Please refer to the attached sample for detailed implementation. Download Sample

MESCIUS inc.

comments powered by Disqus