简体   繁体   English

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

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

I am trying to display the contents of setting.setting file in DataGridView. 我正在尝试在DataGridView中显示setting.setting文件的内容。

I have been successful in doing so by binding the data using BindingSource as follows 通过使用BindingSource如下绑定数据,我已经成功做到了

        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 使用此代码,将使用以下默认值填充我的DataGridView

在此处输入图片说明

Setting Name is read only. Setting Name是只读的。
Settings Value is editable. Settings Value是可编辑的。

I have provided a Save button on the form which has following code in the OnClick event 我在表单上提供了一个Save按钮,该按钮在OnClick事件中具有以下代码

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. Save按钮不会更改settings.settings文件中的值,并且修改的数据在应用程序运行之间不会保留。

My Questions: 我的问题:

  1. What am I doing wrong? 我究竟做错了什么?
  2. How can I get this thing working? 我怎样才能使这个东西正常工作?

Guys any help is really appreciated. 大家的帮助真的很感激。

If using a PropertyGrid is fine also: 如果使用PropertyGrid也可以:

  1. Add a PropertyGrid from the toolbox to the form 将工具箱中的PropertyGrid添加到表单
  2. Double click the form to create the Form_Load event 双击表单以创建Form_Load事件
  3. Add propertyGrid1.SelectedObject = Properties.Settings.Default; propertyGrid1.BrowsableAttributes = new AttributeCollection(new UserScopedSettingAttribute()); 添加propertyGrid1.SelectedObject = Properties.Settings.Default; propertyGrid1.BrowsableAttributes = new AttributeCollection(new UserScopedSettingAttribute()); propertyGrid1.SelectedObject = Properties.Settings.Default; propertyGrid1.BrowsableAttributes = new AttributeCollection(new UserScopedSettingAttribute());
  4. Click on the PropertyGrid and create the PropertyValueChanged event 单击PropertyGrid并创建PropertyValueChanged事件
  5. Add Properties.Settings.Default.Save(); 添加Properties.Settings.Default.Save();
  6. Play around with Designer to style the PropertyGrid eg Dock, PropertySort, HelpVisible, ToolbarVisible 与Designer一起玩以设置PropertyGrid的样式,例如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. 如果设置文件包含范围为“用户”的设置,则应将其显示并保存(如果更改)。

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

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