使用 DataGridView 进行本地设置

发布于 2024-09-16 03:41:54 字数 140 浏览 2 评论 0原文

我需要简单的方法来保存和编辑应用程序中的一组字符串对。我相信具有两个 culmns 的 DataGridView 可以帮助我查看和编辑,并且我希望使用应用程序设置而不是数据库来存储数据(可能是 DataSet 作为设置类型)。我只是无法将所有这些放在一起。有办法吗?

I need simple way to persist and edit a set of string pairs in my application. I believe that DataGridView with two culmns can help me with viewing and editing, and I was hoping to use application settings instead of database to store data (maybe a DataSet as setting type). I just can't put all that together. Is there a way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强者自强 2024-09-23 03:41:54

我是这样做的。 MyMap 类保存值对。它们必须是属性,因为 DatGridView 不适用于字段。 MyMapCollection 保存 MyMap 的集合,作为 BindingList(允许在 DataGridView 中添加行)。需要此类才能使 Visual Studio 设置编辑器满意,但无法使其与普通 BindingList 一起使用。因此:

public class MyMap {
    public String FirstField { get; set; }
    public String SecondField { get; set; }
}

public class MyMapCollection : BindingList<MyMap>
{
    public MyMapCollection Clone()
    {
        MyMapCollection result = new MyMapCollection();

        foreach (MyMap map in this)
            result.Add(new MyMap() {
                FirstField = map.FirstField, SecondField = map.SecondField });

        return result;
    }
}

函数 Clone 创建对象的深层副本,以便在 Settings.Default 中的对象上不会直接更改数据,而是在用户指定时更改。在设置编辑器中,您将添加一个 MyMapCollection 类型的项目,称为 TheValues,并在代码中使用非常简单的它:

myDataGridView.DataSource = Settings.Default.TheValues.Clone();

如果数据应更改回设置(当用户单击“确定”时),则相应地更改设置:

Settings.Default.TheValues = (MyMapCollection)myDataGridView.DataSource;

使用 DataTable 或 DataSet 代替MyMapCollection 也是可能的,但是这个解决方案允许我在代码的其余部分使用 TheValues,这甚至比 DataSet 更甜蜜。

Here's how I did it. Class MyMap holds value pairs. They must be properties, because DatGridView doesn't work with fields. MyMapCollection holds the collection of MyMaps, as BindingList (allows adding rows in DataGridView). This class is needed to make Visual Studio settings editor happy, couldn't make it work with plain BindingList. So:

public class MyMap {
    public String FirstField { get; set; }
    public String SecondField { get; set; }
}

public class MyMapCollection : BindingList<MyMap>
{
    public MyMapCollection Clone()
    {
        MyMapCollection result = new MyMapCollection();

        foreach (MyMap map in this)
            result.Add(new MyMap() {
                FirstField = map.FirstField, SecondField = map.SecondField });

        return result;
    }
}

Function Clone creates a deep copy of the object, so that data is not changed directly on the object in Settings.Default, but when the users says so. In settings editor you would add an item of type MyMapCollection, called say TheValues, and use very simple it in the code:

myDataGridView.DataSource = Settings.Default.TheValues.Clone();

If data should be changed back to settings (when users clicks OK) then change settings accordingly:

Settings.Default.TheValues = (MyMapCollection)myDataGridView.DataSource;

Using a DataTable or DataSet instead of MyMapCollection is also possible, but this solution allows me to use TheValues in the rest of the code, which is even sweeter than DataSet could have been.

预谋 2024-09-23 03:41:54

如果您尝试编辑的值是纯键值对,则可以创建一个将这些值作为属性保存的类,并将此类对象序列化到 XML 文件中。您可以反序列化该类并将值分配给 DataGridView。

您还可以创建自定义配置并将其与 App.config / Web.config 文件分开存储。这类似于 NHibernate 或 spring.Net 配置文件存储在 configsections 键中并引用它们。

这是如何创建您自己的自定义配置的链接。
MSDN 链接

If the values that you are trying to edit are plain key value pairs, you can create a class which holds these values as properties and serialize this class object into an XML file. You can deserialize the class and assign the values to the DataGridView.

You could also create a custom configuration and store it separately from App.config / Web.config file. This will be similar to what NHibernate or spring.Net configuration files are stored with a reference to them in the configsections key.

Here is a link how to creat your own custom configuration.
MSDN link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文