Persist settings for multiple GridViews in one file

2 posts, 0 answers
  1. aborg
    aborg avatar
    5 posts
    Member since:
    Apr 2012

    Posted 22 Jun 2012 Link to this post

    I have an app with quite a few different GridViews all of which need to have their settings persisted. Currently, I am creating a file for each GridView to persist the settings between sessions. Is there a way to consolidate this so that more than one gridview can share a settings file? Here is the current code I have for doing persistence:

    /// <summary>
        /// Interaction logic for BillingActivityView.xaml
        /// </summary>
        public partial class BillingActivityView
        {
            private static readonly string _appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
     
            private static readonly string _bambooSettingsPath = Path.Combine(_appDataPath, @"Bamboo\Settings");
     
            private static readonly string _testsGridSettingsFilePath = Path.Combine(_bambooSettingsPath, @"TestsGridSettings.bin");
     
            private static readonly string _oldBillClassesGridSettingsFilePath = Path.Combine(_bambooSettingsPath, @"OldBillClassesGridSettings.bin");
     
            private static readonly string _insuranceStatusGridSettingsFilePath = Path.Combine(_bambooSettingsPath, @"InsuranceStatusGridSettings.bin");
     
            /// <summary>
            /// Initializes a new instance of the <see cref="DocumentImageView"/> class.
            /// </summary>
            public BillingActivityView()
            {
                InitializeComponent();
                ServiceProvider.RegisterPersistenceProvider<ICustomPropertyProvider>(typeof(RadGridView), new GridViewCustomPropertyProvider());
            }
     
            private void TestsGridView_Initialized(object sender, EventArgs e)
            {
                LoadGridSettings(TestsGridView, _testsGridSettingsFilePath);
            }
     
            private void OldBillClassesGridView_Initialized(object sender, EventArgs e)
            {
                LoadGridSettings(OldBillClassesGridView, _oldBillClassesGridSettingsFilePath);
            }
     
            private void InsuranceStatusGridView_Initialized(object sender, EventArgs e)
            {
                LoadGridSettings(InsuranceStatusGridView, _insuranceStatusGridSettingsFilePath);
            }
     
            public void LoadGridSettings(RadGridView gridView, string settingsPath)
            {
                var manager = new PersistenceManager();
     
                if (!Directory.Exists(_bambooSettingsPath))
                {
                    Directory.CreateDirectory(_bambooSettingsPath);
                }
     
                // Load settings if there is something to load
                if (File.Exists(settingsPath))
                {
                    try
                    {
                        var fileStream = File.OpenRead(settingsPath);
                        manager.Load(gridView, fileStream);
                        fileStream.Close();
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message, "BILLINGACTIVITYVIEW");
                    }
                }
            }
     
            public void PersistAllGridSettings()
            {
                PersistGridSettings(TestsGridView, _testsGridSettingsFilePath);
                PersistGridSettings(OldBillClassesGridView, _oldBillClassesGridSettingsFilePath);
                PersistGridSettings(InsuranceStatusGridView, _insuranceStatusGridSettingsFilePath);
            }
     
            public void PersistGridSettings(RadGridView gridView, string settingsPath)
            {
                var manager = new PersistenceManager();
                var stream = manager.Save(gridView);
     
                using (var fileStream = File.Create(settingsPath))
                {
                    stream.CopyTo(fileStream);
                }
            }
        }
  2. Lancelot
    Lancelot avatar
    251 posts
    Member since:
    Jul 2012

    Posted 26 Jun 2012 Link to this post

    Hi Brian,

      This would be the perfect opportunity for you to leverage the power and flexibility of the RadPersistenceFramework. This link will bring you the Getting Started page in the help documentation. With just a couple lines of code you can persist the settings of all your RadGridView instances.

    Good Luck!
    Lancelot
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top