Edit contents of Setting File (settings.settings) in DataGridView

1.5k views Asked by At

I am trying to display the contents of setting.setting file in DataGridView.

I have been successful in doing so by binding the data using BindingSource as follows

        BindingSource bindingSource1 = new BindingSource();
        bindingSource1.DataSource = Properties.user.Default.Properties;
        settingsDataGridView.DataSource = bindingSource1;

Using this code, my DataGridView gets populated with the default values as below

enter image description here

Setting Name is read only.
Settings Value is editable.

I have provided a Save button on the form which has following code in the OnClick event

Properties.user.Default.Save();

The idea is to give control to the user to change the settings using a simple interface.

Unfortunately,this does not do the trick. Save button does not change the values in settings.settings file and the modified data does not persists between the application runs.

My Questions:

  1. What am I doing wrong?
  2. How can I get this thing working?

Guys any help is really appreciated.

1

There are 1 answers

0
A.J.Bauer On

If using a PropertyGrid is fine also:

  1. Add a PropertyGrid from the toolbox to the form
  2. Double click the form to create the Form_Load event
  3. Add propertyGrid1.SelectedObject = Properties.Settings.Default; propertyGrid1.BrowsableAttributes = new AttributeCollection(new UserScopedSettingAttribute());
  4. Click on the PropertyGrid and create the PropertyValueChanged event
  5. Add Properties.Settings.Default.Save();
  6. Play around with Designer to style the PropertyGrid e.g. Dock, PropertySort, HelpVisible, ToolbarVisible

Code should look similar to this:

using System;
using System.ComponentModel;
using System.Configuration;
using System.Windows.Forms;

namespace YourAppNamespace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            propertyGrid1.SelectedObject = Properties.Settings.Default;
            propertyGrid1.BrowsableAttributes = new AttributeCollection(new UserScopedSettingAttribute());
        }

        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            Properties.Settings.Default.Save();
        }
    }
}

If the Settings file contains settings with Scope "User" then they should be displayed and saved if changed.