我对c#中的datagridview选项有疑问,我有一个项目,我需要将.INI文件的内容放在datagridview中。我之前所做的就是我将ini文件的信息放在1个列表框和4个文本框中,但结果却是一团糟。这就是我试图把它放在datagridvieuw中的原因。有人可以帮助我。
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,用于保存和拉动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);
}
}
}
提前致谢。
问题
如何将信息放入ini文件的datagridvieuw中。
修改
名称
彼得
约翰
阿诺德
答案 0 :(得分:0)
首先,您可以改进读取ini文件的方法。 This link会给你一些好主意。
其次,您可以将ini文件的信息读入datatable。然后将数据表绑定到datagridview。