C# 在DataGridView中编辑设置文件(settings.settings)的内容

C# 在DataGridView中编辑设置文件(settings.settings)的内容,c#,.net,winforms,datagridview,settings.settings,C#,.net,Winforms,Datagridview,Settings.settings,我试图在DataGridView中显示setting.setting文件的内容 通过使用BindingSource绑定数据,我成功地做到了这一点,如下所示 BindingSource bindingSource1 = new BindingSource(); bindingSource1.DataSource = Properties.user.Default.Properties; settingsDataGridView.DataSource

我试图在DataGridView中显示setting.setting文件的内容

通过使用BindingSource绑定数据,我成功地做到了这一点,如下所示

        BindingSource bindingSource1 = new BindingSource();
        bindingSource1.DataSource = Properties.user.Default.Properties;
        settingsDataGridView.DataSource = bindingSource1;
使用这段代码,我的DataGridView将填充默认值,如下所示

设置名称
为只读。
设置值
可编辑

我在表单上提供了一个
Save
按钮,其中包含以下代码
OnClick
事件

Properties.user.Default.Save();
这样做的目的是让用户通过一个简单的界面来控制更改设置

不幸的是,这并不奏效<代码>保存按钮不会更改
设置中的值。设置
文件和修改的数据在应用程序运行之间不会持续

我的问题是:

  • 我做错了什么
  • 我怎样才能让它工作

  • 伙计们,非常感谢你们的帮助

    如果使用PropertyGrid也可以:

  • 将工具箱中的PropertyGrid添加到表单中
  • 双击表单以创建表单加载事件
  • 添加
    propertyGrid1.SelectedObject=Properties.Settings.Default;
    propertyGrid1.BrowsableAttributes=new AttributeCollection(new UserScopedSettingAttribute())
  • 单击PropertyGrid并创建PropertyValueChanged事件
  • 添加
    Properties.Settings.Default.Save()
  • 使用Designer设置属性网格的样式,例如Dock、PropertySort、HelpVisible、ToolbarVisible
  • 代码应类似于以下内容:

    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();
            }
        }
    }
    

    如果设置文件包含范围为“用户”的设置,则应在更改后显示并保存这些设置。

    是否将这些设置标记为用户设置而不是应用程序设置?@heribertolougo:是的,是“用户”设置用户范围的设置未存储在应用程序文件夹中的设置文件中,尽管奇怪的是,你抱怨在应用程序运行之间没有保留设置。您是否尝试在调试环境之外以发布模式构建和运行应用程序,并查看结果是否不同?也就是说,不要在visual studio中单击“播放”,构建应用程序,然后像用户一样双击exe,更改一些设置并通过双击重新启动应用程序,就像用户将脱离主题一样,而是使用
    PropertyGrid
    。@CaiusJard:刚才,通过双击它尝试运行应用程序(发布版本),但没有成功。它不是在保留这些值。