我想在输入数据后将datagridview转换为ini文件。 [英] I want to covert the datagridview into an ini file after inputting the data.

查看:183
本文介绍了我想在输入数据后将datagridview转换为ini文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.hoons.net/Board/qacshap/Content/67073



当我输入上面的URL时,



我尝试将数据放入网格并按导出按钮以section,key,value的形式将其保存为.ini文件。我该怎么办?在代码中,内容被创建为ini文件,但不是网格。



我尝试过:



http://www.hoons.net/Board/qacshap/Content/67073

When I enter the URL above,

I try to put data into the grid and press the export button to save it as an .ini file in the form of section, key, value. What should I do? Inside the code, the content is created as an ini file, but not as a grid.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace EXPORT
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        [DllImport('kernel32')]

        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport('kernel32')]

        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        static string path = 'C:\\Test.ini';
        public Form1()
        {
            InitializeComponent();
            dataGridView1.AllowUserToAddRows =true; //자동 행 추가
            dataGridView1.AutoGenerateColumns = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WritePrivateProfileString('SECTION', 'KEY', 'VALUE', @'C:\ConnectionInfo.ini');
            MessageBox.Show('EXPORT successfully to *.INI format');
           

        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {


        }
        private void WriteInFile(string section,string key,string value,string path)
        {
            WritePrivateProfileString(section, key, value, path);
            if (value == null)
            {
                throw new ArgumentException();
            }
        }

        private void button2_Click(object sender, EventArgs e)  //ADD_ROW Button
        {
            DataGridViewButtonColumn button = new DataGridViewButtonColumn();
            {
                dataGridView1.Rows.Add();
            }
        }
    }
}

推荐答案

如前所述,INI文件不适合存储基于表的数据:它们是一种平面文件格式,实际上并不是为多行设置的(特别是对于包含多列的行)。它们是(过时的)初始化文件格式,而不是数据文件格式。



您应该查看XML,JSON或真正的数据库:SQL Server ,MySQL,Access,SQLite,...这里有很多不同的可能性。



另一件事是你不想保存DataGridView:你想要保存它的基础数据源,因为现代应用程序中的数据独立于表示媒介(用户与之交互的形式等)

当您将数据加载到应用程序中时,加载数据源(几乎可以是任何集合,但List或DataTable可能是最常见的) - 当您保存数据时,您将保存数据源。大多数控件(例如DataGridView)都是数据感知的,并且可以让您将数据源直接绑定到控件,以便它将如何显示数据。通常,这是一行代码!

As has been mentioned, INI files are not a "good fit" for storing table based data: they are a "flat file" format that really isn't set up for multiple rows (or especially for rows which contain multiple columns). They are an (outdated) initialization file format, not a data file format.

You should instead look at XML, JSON, or a "true" database: SQL Server, MySQL, Access, SQLite, ... there are many of different possibilities here.

The other thing is that you don't want to save the DataGridView: you want to save it's underlying Data Source instead, because the data in modern apps is independent of the presentation medium (the form and such like that the user interacts with)
When you load data into the app, you load a datasource (which can be pretty much any collection but a List or a DataTable are probably the most common) - and when you save the data, you save the Data Source. Most controls - such as the DataGridView - are data aware, and will let you "bind" the Data Source directly to the control so that it will sort out how to display the data. Normally, that's one line of code!
myDataGridView.DataSource = MyCollection;

我强烈建议您阅读数据绑定 - Google会有所帮助 - 走那条路。从长远来看,它会更快,更可靠,并生成更易维护的代码。

I would strongly suggest that you read up on data binding - Google will help - and go that route. It'll be quicker, more reliable, and produce more maintainable code in the long run.


数据网格是一种编辑.INI文件的可怕方式。



要编写.INI文件,您将自己解析数据网格的每一行,跟踪您所在的部分,并构建条目字典(键/值)对)写入.INI。一旦你拥有了这两个数据,你就可以继续编写.INI文件。



这里不会有这样的例子。在数据网格中编辑和构建.INI文件是一种可怕的方法。
A datagrid is a terrible way to edit an .INI file.

To write the .INI file, you're going to have parse each line of the datagrid yourself, keep track of which section you're in, and build a dictionary of entries (key/value pairs) to write to the .INI. Once you have both of those pieces of data, you can then move on to writing the .INI file.

There are not going to be an examples of this. Editing and building an .INI file in a datagrid is a terrible way to do it.


这篇关于我想在输入数据后将datagridview转换为ini文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆