none
i want to conver the datagridview into an .ini file ...after ini covert button RRS feed

  • Question

  • 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.

    I want to see the generated ini file by putting it in the datagridview and pressing the export button instead of putting section,

    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();
                }
            }
        }
    }

    Tuesday, April 23, 2019 4:57 AM

Answers

  • Hi

    Thank you for posting here.

    For your question, you want to put data into the grid and press the export button to save it as an .ini file.

    You could try the following code.

    private void Form1_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add("SECTION", typeof(string));
                table.Columns.Add("KEY", typeof(string));
                table.Columns.Add("VALUE", typeof(string));
                table.Rows.Add("a", "123", "456");
                table.Rows.Add("", "789", "234");
                table.Rows.Add("", "345", "678");
               table.Rows.Add("b", "123", "456");
                table.Rows.Add("", "789", "234");
                table.Rows.Add("", "345", "678");
                dataGridView1.DataSource = table;
            }
            private void Button1_Click(object sender, EventArgs e)
            {
              
                    for (int j = 0; j <dataGridView1.RowCount-1; j++)
                    {
                        if(j<=2)
                        {
                            WritePrivateProfileString(dataGridView1[0, 0].Value.ToString(), dataGridView1[1, j].Value.ToString(), dataGridView1[2, j].Value.ToString(), @"D:\ConnectionInfo.ini");
                        }
                        else
                        {
                            WritePrivateProfileString(dataGridView1[0, 3].Value.ToString(), dataGridView1[1, j].Value.ToString(), dataGridView1[2, j].Value.ToString(), @"D:\ConnectionInfo.ini");
                        }
    
                    }
                MessageBox.Show("EXPORT successfully to *.INI format");
            }
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by chh.552 Tuesday, April 23, 2019 7:30 AM
    Tuesday, April 23, 2019 6:50 AM
    Moderator

All replies

  • Hi

    Thank you for posting here.

    For your question, you want to put data into the grid and press the export button to save it as an .ini file.

    You could try the following code.

    private void Form1_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add("SECTION", typeof(string));
                table.Columns.Add("KEY", typeof(string));
                table.Columns.Add("VALUE", typeof(string));
                table.Rows.Add("a", "123", "456");
                table.Rows.Add("", "789", "234");
                table.Rows.Add("", "345", "678");
               table.Rows.Add("b", "123", "456");
                table.Rows.Add("", "789", "234");
                table.Rows.Add("", "345", "678");
                dataGridView1.DataSource = table;
            }
            private void Button1_Click(object sender, EventArgs e)
            {
              
                    for (int j = 0; j <dataGridView1.RowCount-1; j++)
                    {
                        if(j<=2)
                        {
                            WritePrivateProfileString(dataGridView1[0, 0].Value.ToString(), dataGridView1[1, j].Value.ToString(), dataGridView1[2, j].Value.ToString(), @"D:\ConnectionInfo.ini");
                        }
                        else
                        {
                            WritePrivateProfileString(dataGridView1[0, 3].Value.ToString(), dataGridView1[1, j].Value.ToString(), dataGridView1[2, j].Value.ToString(), @"D:\ConnectionInfo.ini");
                        }
    
                    }
                MessageBox.Show("EXPORT successfully to *.INI format");
            }
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by chh.552 Tuesday, April 23, 2019 7:30 AM
    Tuesday, April 23, 2019 6:50 AM
    Moderator
  • That's the way I want it. By the way

    One thing ... if I have the same key and value in the same session, can I make the section go out without repeating?

    example)
    section        key         value
    [app_info]   name        a
                   prd_name     b

    .ini->
    [app_info]
    name = a
    prd_name = b
    • Edited by chh.552 Tuesday, April 23, 2019 7:13 AM
    Tuesday, April 23, 2019 7:12 AM
  • Hi

    Thanks for the feedback.

    One thread can only reply to one question, so that it will help other members to find the solution quickly if they face a similar issue. You could mark it as an answer and create a new thread in c# forum. We will solve your new problem in another thread.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by chh.552 Tuesday, April 23, 2019 7:54 AM
    • Unmarked as answer by chh.552 Tuesday, April 23, 2019 7:57 AM
    Tuesday, April 23, 2019 7:29 AM
    Moderator