0

i have an question about the datagridview option in c#, i have an project and i need to put the content of an .INI file in an datagridview. What i've don before this is that i put the information of the ini file in 1 listbox and 4 textboxes, But this turned out to be a mess. That is why i tried to put it in an datagridvieuw. Can someone please help me.

Maincode:

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

        }

    }
}

toepassinginifile, For saving and pulling from inifile

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

Thanks in advance.

QUESTION

How to i put the info in an datagridvieuw from an ini file.

Edit Name
Peter John Arnold

  • What is the problem? I don't see a question anywhere. – Bridge Aug 28 '13 at 08:24
  • Question is in the edit –  Aug 28 '13 at 08:53
  • i think puttin it in a list is the easiest way to go but how to i do that –  Aug 28 '13 at 08:54
  • Your "question" still appears to be "someone write the code for me". Without reading this big pile of (mostly irrelevant) code, can you please summarise what you tried, and what went wrong? – Bridge Aug 28 '13 at 08:56
  • i have tried it with a list <> and i works pretty good if i say it myself. But it has to appear multiple times like in the edit –  Aug 28 '13 at 09:07
  • but it only shows peter with peters properties like age and length –  Aug 28 '13 at 09:09

1 Answers1

0

First you can improve the method of reading ini file. This link will give you some good ideas.

Second you can read the information of the ini file into datatable. Then bind the datatable to the datagridview.

Community
  • 1
  • 1
huoxudong125
  • 1,966
  • 2
  • 26
  • 42