簡體   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