C# 如何在datagridview中放置.ini文件

C# 如何在datagridview中放置.ini文件,c#,datagridview,C#,Datagridview,我对c#中的datagridview选项有一个问题,我有一个项目,需要将.INI文件的内容放在datagridview中。在这之前我没有做的是,我把ini文件的信息放在1个列表框和4个文本框中,但结果是一团糟。这就是为什么我试着把它放在datagridvieuw中。谁能帮帮我吗 main代码: // Author Collin Koornstra using System; using System.Data; using System.IO; using System.Windows.Form

我对c#中的datagridview选项有一个问题,我有一个项目,需要将.INI文件的内容放在datagridview中。在这之前我没有做的是,我把ini文件的信息放在1个列表框和4个文本框中,但结果是一团糟。这就是为什么我试着把它放在datagridvieuw中。谁能帮帮我吗

main代码:

// Author Collin Koornstra
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
using Communication;

namespace Flashloader
{
    public partial class MainForm : Form
    {
        private controllerinifile _controllerIniFile;
        private toepassinginifile _toepassingIniFile;

        //private bool _changed;

        public MainForm()
        {
            InitializeComponent();



            MaximizeBox = false;

            _controllerIniFile = new controllerinifile();
            _toepassingIniFile = new toepassinginifile(_controllerIniFile.Controllers);

            Refreshall();

            _applicationListBox.SelectedItem = _toepassingIniFile.ToePassingen.FindByName(_toepassingIniFile.Settings.LastUsed);
        }

        private void AppFile()
        {
            Toepassing toepassing = GetCurrentApplication();
            if (toepassing == null)
            {
                MessageBox.Show("No application selected");
                return;
            }
            OpenFileDialog appfile = new OpenFileDialog();
            appfile.Filter = "Srec Files (.a20; .a21; .a26; .a44)|*.a20; *.a21; *.a26; *.a44|All files (*.*)|*.*";
            appfile.Title = ("Choose a file");
            appfile.FileName = toepassing.Lastfile;
            appfile.InitialDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), @"C:\\\\Projects\\flashloader2013\\mainapplication\\");
            appfile.RestoreDirectory = true;
            if (appfile.ShowDialog() == DialogResult.OK)
            {
                GetCurrentApplication().Lastfile = _appFileTextBox.Text = appfile.FileName;
               // _changed = true;
            }
        }

        // File select Button and file directory
        private void Button2Click(object sender, EventArgs e)
        {
            AppFile();

        }

        private void SendFileButtonClickedEvent(object sender, EventArgs e)
        {

            Toepassing toepassing = GetCurrentApplication();
            if (toepassing == null)
            {
                MessageBox.Show("No Application selected.");
                return;
            }

            if (!File.Exists(_appFileTextBox.Text) )
            {
                MessageBox.Show("Applicationfile doesn't exist:"+ _appFileTextBox.Text);
                return;
            }

            String bootFileName = _bootFileTextBox.Text;
            String appFileName = _appFileTextBox.Text;


            Settings settings = _toepassingIniFile.Settings;
            FileTransfer transfer = new FileTransfer(settings.Port, settings.Baudrate, bootFileName, appFileName);
            transfer.Run();
            // transfer.Worker(null, null);


        }



        // Saving the settings to the INI file.
        private void SaveSettings()
        {
            _toepassingIniFile.Save(GetCurrentApplication());
        }

        //Refresh function
        private void Refreshall()
        {
            // Filling the listbox and the combobox
            //Setting Comse
            _comPortTextBox.Text = _toepassingIniFile.Settings.Port;
            _baudRateTextBox.Text = _toepassingIniFile.Settings.Baudrate;

            // refresh controllers and applications
            _controllercombobox.DataSource = null;
            _controllercombobox.DataSource = _controllerIniFile.Controllers;
            _applicationListBox.DataSource = null;
            _applicationListBox.DataSource = _toepassingIniFile.ToePassingen;


        }

        // refreshing application
        private void AddNewApplication()
        {
            var newapplication = new NewApplication(_toepassingIniFile);
            if (newapplication.Run())
            {
                _applicationListBox.DataSource = null;
                _applicationListBox.DataSource = _toepassingIniFile.ToePassingen;
                _applicationListBox.SelectedIndex = _toepassingIniFile.ToePassingen.Count - 1;
                _controllercombobox.DataSource = null;
                _controllercombobox.DataSource = _controllerIniFile.Controllers;
            }
        }

        //delete function
        private void DeleteFromListbox()
        {
            var application = this.GetCurrentApplication();

            if (application == null)
            {
                MessageBox.Show("No Application selected");
                return;
            }
            if (MessageBox.Show("You are about to delete application: " + Environment.NewLine + _applicationListBox.SelectedItem + Environment.NewLine + "Are you sure you want to delete the application?", "", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                MessageBox.Show("The application will not be deleted.", "", MessageBoxButtons.OK);
            }
            else if (this._applicationListBox.SelectedIndex >= 0)
            {
                int index = _applicationListBox.SelectedIndex;

                _toepassingIniFile.ToePassingen.Remove(application);
                if (index == _toepassingIniFile.ToePassingen.Count)
                    --index;
                application = index < 0 ? null : _toepassingIniFile.ToePassingen[index];

                _toepassingIniFile.Save(application);

                _applicationListBox.DataSource = null;
                _applicationListBox.DataSource = _toepassingIniFile.ToePassingen;

                _applicationListBox.SelectedIndex = index;


            }
        }

        //Getcurrentcontroller functie
        private Controller GetCurrentController()
        {
            int index = _controllercombobox.SelectedIndex;
            if (index < 0)
                return null;
            return _controllerIniFile.Controllers[index];
        }

        //Getcurrentapplication funtie
        private Toepassing GetCurrentApplication()
        {
            int index = _applicationListBox.SelectedIndex;
            if (index < 0)
                return null;
            return _toepassingIniFile.ToePassingen[index];
        }

        // Selected application settings
        private void TypelistboxSelectedIndexChanged(object sender, EventArgs e)
        {
            Toepassing toepassing = GetCurrentApplication();
            if (toepassing == null)
            {
                _controllercombobox.SelectedIndex = -1;
                _appFileTextBox.Text = "";
            }
            else
            {
                _appFileTextBox.Text = toepassing.Lastfile;
                _controllercombobox.SelectedItem = toepassing.Controller;

            }
        }

        // Selected controller settings
        private void ControllercomboboxSelectedIndexChanged(object sender, EventArgs e)
        {
            Toepassing toepassing = GetCurrentApplication();
            Controller controller = GetCurrentController();
            if (controller == null)
            {
                _bootFileTextBox.Text = "";
            }
            else
            {
                _bootFileTextBox.Text = controller.Bootfile;
                if (toepassing != null)
                    toepassing.Controller = controller;
            }
           // _changed = true;
        }


        private void CloseApplicationEvent(object sender, EventArgs e)
        {
            Close();
        }

        private void ShowControllersEvent(object sender, EventArgs e)
        {
            var controlleredit = new ControllerListForm(_controllerIniFile);
            controlleredit.ShowDialog();
            Refreshall();
        }

        private void AddNewControllerEvent(object sender, EventArgs e)
        {
            var controllersettings = new Newcontroller(_controllerIniFile);
            controllersettings.ShowDialog();
            Refreshall();
        }


        private void NewapplicationBttonClick(object sender, EventArgs e)
        {
            AddNewApplication();
        }


        private void DeleteApllicationEvent(object sender, EventArgs e)
        {
            DeleteFromListbox();
        }


        private void CreateNewApllicationEvent(object sender, EventArgs e)
        {
            AddNewApplication();
        }


        private void ComPortSettingsEvent(object sender, EventArgs e)
        {
            var comsettings = new ComSettings(_toepassingIniFile.Settings);
            if (comsettings.Run())
            {
                _toepassingIniFile.Save(GetCurrentApplication());
                Refreshall();
            }
        }

        private void ApplicationListBoxPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DeleteFromListbox();
            }
            if (e.KeyCode == Keys.Insert)
            {
                AddNewApplication();
            }

        }

    }
}
//作者柯林·库恩斯特拉
使用制度;
使用系统数据;
使用System.IO;
使用System.Windows.Forms;
使用通信;
命名空间Flashloader
{
公共部分类主窗体:窗体
{
私有控制器文件(controllerinifile);;
私人Toepassignile(私人Toepassignile);;
//私人住宅发生了变化;
公共表格(
{
初始化组件();
MaximizeBox=假;
_controllerIniFile=新的controllerIniFile();
_TOEPASSINGILE=新的TOEPASSINGILE(_controllerIniFile.Controllers);
Refreshall();
_applicationListBox.SelectedItem=\u ToePassingIfile.ToePassingen.FindByName(\u ToePassingIfile.Settings.LastUsed);
}
私有void AppFile()
{
Toepassing-Toepassing=GetCurrentApplication();
if(toepassessing==null)
{
MessageBox.Show(“未选择任何应用程序”);
返回;
}
OpenFileDialog appfile=新建OpenFileDialog();
appfile.Filter=“Srec文件(.a20;.a21;.a26;.a44)|*.a20;*.a21;*.a26;*.a44 |所有文件(*.*)|*.*”;
appfile.Title=(“选择文件”);
appfile.FileName=toepassing.Lastfile;
appfile.InitialDirectory=System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath),@“C:\\\\Projects\\flashloader2013\\mainapplication\\”;
appfile.RestoreDirectory=true;
if(appfile.ShowDialog()==DialogResult.OK)
{
GetCurrentApplication().Lastfile=\u appFileTextBox.Text=appfile.FileName;
//_changed=true;
}
}
//文件选择按钮和文件目录
私有无效按钮2单击(对象发送者,事件参数e)
{
AppFile();
}
私有void SendFileButtonClickedEvent(对象发送者,事件参数e)
{
Toepassing-Toepassing=GetCurrentApplication();
if(toepassessing==null)
{
MessageBox.Show(“未选择任何应用程序”);
返回;
}
如果(!File.Exists(_appFileTextBox.Text))
{
MessageBox.Show(“应用程序文件不存在:”+\u appFileTextBox.Text);
返回;
}
字符串bootFileName=\u bootFileTextBox.Text;
字符串appFileName=\u appFileTextBox.Text;
设置设置=\u到Passingifile.Settings;
FileTransfer=新文件传输(settings.Port、settings.Baudrate、bootFileName、appFileName);
transfer.Run();
//transfer.Worker(null,null);
}
//将设置保存到INI文件。
私有void SaveSettings()
{
_保存(GetCurrentApplication());
}
//刷新功能
私有void Refreshall()
{
//填充列表框和组合框
//安眠药
_comPortTextBox.Text=\u到passinginifile.Settings.Port;
_波特率文本框.Text=\u到passinginifile.Settings.Baudrate;
//刷新控制器和应用程序
_controllercombobox.DataSource=null;
_controllercombobox.DataSource=\u controllerIniFile.Controllers;
_applicationListBox.DataSource=null;
_applicationListBox.DataSource=\u toepassingiFile.ToePassingen;
}
//刷新应用程序
私有void AddNewApplication()
{
var newapplication=新的newapplication(_topassingifile);
if(newapplication.Run())
{
_applicationListBox.DataSource=null;
_applicationListBox.DataSource=\u toepassingiFile.ToePassingen;
_applicationListBox.SelectedIndex=\u ToePassingIfile.ToePassingen.Count-1;
_controllercombobox.DataSource=null;
_controllercombobox.DataSource=\u controllerIniFile.Controllers;
}
}
//删除函数
私有void DeleteFromListbox()
{
var application=this.GetCurrentApplication();
if(应用程序==null)
{
MessageBox.Show(“未选择任何应用程序”);
返回;
}
如果(MessageBox.Show(“您将要删除应用程序:+Environment.NewLine+\u applicationListBox.SelectedItem+Environment.NewLine+“是否确实要删除该应用程序?”,“”,MessageBoxButtons.YesNo)=DialogResult.No)
{
MessageBox.Show(“应用程序将不会被删除。”,“”,MessageBoxButtons.OK);
}
else if(此.\u applicationListBox.SelectedIndex>=0)
{
int index=\u applicationListBox.SelectedIndex;
_toepassingile.ToePassingen.Remove(应用);
if(index==\u toepassingile.ToePassingen.Count)
--指数;
application=index<0?null:_toepassingifile.ToePassingen[index];
_toepassignile.Save(应用程序);
_applicationListBox.DataSource=null;
_applicationListBox.DataSource=\u toepassingiFile.ToePassingen;
_applicationListBox.SelectedIndex=索引;
}
}
//Getcurrentcontroller函数
专用控制器GetCurrentController()
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Idento.Common.Utilities;

namespace Flashloader
{
    public class toepassinginifile
    {
        private const String FILE_NAME = "flash.ini";
        private Controllerlist _controllers;
        public Toepassinglist ToePassingen { get; set; }
        public Settings Settings { get; private set; }


        public toepassinginifile(Controllerlist controllers)
        {
            _controllers = controllers;

            // TODO Startup class maken en laden
            ToePassingen = LoadToepassingen();
        }

        private Toepassinglist LoadToepassingen()
        {
            StringList input = new StringList().FromFile(FILE_NAME);
            Toepassinglist output = new Toepassinglist();

            Settings settings = null;
            Toepassing toepassing = null;

            foreach (var item in input)
            {
                String line = item.Trim();

                if (line.StartsWith("[") && line.EndsWith("]"))
                {
                    settings = new Settings();
                    toepassing = null;

                    String name = line.Substring(1, line.Length - 2);

                    if (name.ToUpper().Equals("STARTUP"))
                    {
                        Settings = settings = new Settings();
                        continue;
                    }

                    // TODO kan weg in de toekomst
                    if (name.ToUpper().Equals("DRAG && DROP"))
                    {
                        toepassing = null;
                        continue;
                    } // */

                    toepassing = new Toepassing(name);
                    settings = null;
                    output.Add(toepassing);
                }

                else if (settings != null)
                {
                    int index = line.IndexOf('=');
                    if (index < 0)
                        continue;

                    String key = line.Substring(0, index).Trim();
                    String value = line.Substring(index + 1).Trim();

                    if (Utils.EqualsIgnoreCase(key, "Baudrate"))
                        settings.Baudrate = value;
                    else if (Utils.EqualsIgnoreCase(key, "Port"))
                        settings.Port = value;
                    else if (Utils.EqualsIgnoreCase(key, "LastUsed"))
                        settings.LastUsed = value;
                }
                else if (toepassing != null)
                {
                    int index = line.IndexOf('=');
                    if (index < 0)
                        continue;

                    String key = line.Substring(0, index).Trim();
                    String value = line.Substring(index + 1).Trim();

                    if (Utils.EqualsIgnoreCase(key, "TabTip"))
                        toepassing.TabTip = value;
                    else if (Utils.EqualsIgnoreCase(key, "Controller"))
                        toepassing.Controller = _controllers.FindByName(value);
                    else if (Utils.EqualsIgnoreCase(key, "Lastfile"))
                        toepassing.Lastfile = value;
                    else if (Utils.EqualsIgnoreCase(key, "Articlenumber"))
                        toepassing.Articlenumber = value;
                    else if (Utils.EqualsIgnoreCase(key, "Useboot"))
                        toepassing.Useboot = value;
                }
            }
            return output;
        }

        public void Save(Toepassing lastUsed)
        {
            StringList list = new StringList();

            // Toepassing settings = new Toepassing("[Startup]");

            list.Add("[" + Settings.Name + "]");
            list.Add("LastUsed=" + (lastUsed == null ? "" : lastUsed.Name));
            list.Add("Port=" + Settings.Port);
            list.Add("Baudrate=" + Settings.Baudrate);

            foreach (Toepassing item in ToePassingen)
            {
                list.Add("");
                list.Add("[" + item.Name + "]");
                list.Add("Articlenumber=" + item.Articlenumber);
                list.Add("Controller=" + item.Controller.Name);
                list.Add("TabTip=" + item.TabTip);
                list.Add("LastFile=" + item.Lastfile);
               // list.Add("UseBoot=" + item.Useboot);
            }

            list.ToFile(FILE_NAME);
        }
    }
}