繁体   English   中英

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

[英]Edit contents of Setting File (settings.settings) in DataGridView

我正在尝试在DataGridView中显示setting.setting文件的内容。

通过使用BindingSource如下绑定数据,我已经成功做到了

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

使用此代码,将使用以下默认值填充我的DataGridView

在此处输入图片说明

Setting Name是只读的。
Settings Value是可编辑的。

我在表单上提供了一个Save按钮,该按钮在OnClick事件中具有以下代码

Properties.user.Default.Save();

想法是让用户使用简单的界面来更改设置。

不幸的是,这并不能解决问题。 Save按钮不会更改settings.settings文件中的值,并且修改的数据在应用程序运行之间不会保留。

我的问题:

  1. 我究竟做错了什么?
  2. 我怎样才能使这个东西正常工作?

大家的帮助真的很感激。

如果使用PropertyGrid也可以:

  1. 将工具箱中的PropertyGrid添加到表单
  2. 双击表单以创建Form_Load事件
  3. 添加propertyGrid1.SelectedObject = Properties.Settings.Default; propertyGrid1.BrowsableAttributes = new AttributeCollection(new UserScopedSettingAttribute()); propertyGrid1.SelectedObject = Properties.Settings.Default; propertyGrid1.BrowsableAttributes = new AttributeCollection(new UserScopedSettingAttribute());
  4. 单击PropertyGrid并创建PropertyValueChanged事件
  5. 添加Properties.Settings.Default.Save();
  6. 与Designer一起玩以设置PropertyGrid的样式,例如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();
        }
    }
}

如果设置文件包含范围为“用户”的设置,则应将其显示并保存(如果更改)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM