C# DataGridView Export

2 May
My .ini Magaer using DataGridView

My .ini Magaer using DataGridView

I need to manage a .ini file that contains data in the format id=value. There is some information in the first 6 lines that I deal with in my code, but for the most part the data can be dealt with by splitting on ‘=’.

This app loads the .ini in to a DataGridView and allows the user to search, edit, add or remove records. Lastly, any changes can be exported out to a new .ini file.

Here is the code to create a DataGridView, search columns, clear highlighted rows and save as text. Each function is easy to find as it is attached to a buttons onclick.

Here is the code for the form:

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.IO;

namespace ini
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();

 

dataGrid.AutoGenerateColumns = false;

DataGridViewTextBoxColumn titleColumn = new DataGridViewTextBoxColumn();
titleColumn.DataPropertyName = “SN”;
titleColumn.HeaderText = “Machine Name”;

DataGridViewTextBoxColumn postColumn = new DataGridViewTextBoxColumn();
postColumn.DataPropertyName = “Terminal”;
postColumn.HeaderText = “Terminal ID”;
dataGrid.Columns.Add(titleColumn);
dataGrid.Columns.Add(postColumn);

dataGrid.AllowUserToOrderColumns = true;
dataGrid.AllowUserToDeleteRows = true;
dataGrid.GridColor = Color.DarkSlateGray;

var reader = new StreamReader(File.OpenRead(“Terminal.ini”));
List<string> listA = new List<string>();
List<string> listB = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(‘=’);

if (line.Contains(“=”))
{
if (values[0] == null)
{
values[0] = “EMPTY”;
dataGrid.Rows.Add(values[0], values[1]);
}
else
{
dataGrid.Rows.Add(values[0], values[1]);
}
}
else
{

}

//Maybe use this later to search?
//listA.Add(values[0]);
//listB.Add(values[1]);

}
}

private void button1_Click(object sender, EventArgs e)
{
string searchValue = textBox1.Text;

dataGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in dataGrid.Rows)
{
if (row.Cells[0].Value.ToString().Contains(searchValue))
{
row.Selected = true;

}

}

}
catch (Exception exc)
{

}
}

private void button4_Click(object sender, EventArgs e)
{
string searchValue = textBox1.Text;

dataGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in dataGrid.Rows)
{
if (row.Cells[1].Value.ToString().Contains(searchValue))
{
row.Selected = true;

}

}

}
catch (Exception exc)
{

}
}

private void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGrid.Rows)
{
row.Selected = false;
}
}

private void button3_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(“Terminal-fromApp.ini”))
{

try
{
System.IO.File.Delete(“Terminal-fromApp.ini”);
}
catch (System.IO.IOException excep)
{
Console.WriteLine(excep.Message);
return;
}
}

StringBuilder myString = new StringBuilder();
StreamWriter sw = new StreamWriter(“Terminal-fromApp.ini”, true);
// var headers = dataGrid.Columns.Cast<DataGridViewColumn>();
myString.Append(“[Settings]\r\n”+”START=GUI1\r\n”+”MAX=G999\r\n”+”[Mappings]\r\n” );
myString.AppendLine();

foreach (DataGridViewRow row in dataGrid.Rows)
{
var cells = row.Cells.Cast<DataGridViewCell>();
myString.Append(string.Join(“=”, cells.Select(cell => cell.Value )));
myString.AppendLine();

}
string q = myString.ToString();
string xx = q.Substring(0,q.LastIndexOf(‘=’));
sw.WriteLine(xx);
sw.Close();
MessageBox.Show(“File Exported”, “Done”);
}
}
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: