[英]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: 我的问题:
Guys any help is really appreciated. 大家的帮助真的很感激。
If using a PropertyGrid is fine also: 如果使用PropertyGrid也可以:
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());
Properties.Settings.Default.Save();
Properties.Settings.Default.Save();
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.