Hi luckydead,
Please refer below sample.
Namespaces
C#
using System.IO;
using System.Text;
using System.Data;
VB.Net
Imports System.IO
Imports System.Text
Imports System.Data
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
dataGridView.AllowUserToAddRows = false;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Lines"),
new DataColumn("CGTypes"),
new DataColumn("Codes")});
dt.Rows.Add("Line1", "IM08+IU08+IM99+IU16", "20,14,A");
dt.Rows.Add("Line2", "IM08+IU08+IM99+IU16", "20,14,A");
dt.Rows.Add("Line3", "IM08+IU08+IM89+IU14", "20,14,A");
dataGridView.DataSource = dt;
}
private void OnSave(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (DataGridViewRow row in dataGridView.Rows)
{
string line = row.Cells[0].FormattedValue.ToString();
string codes = row.Cells[2].FormattedValue.ToString();
sb.Append(string.Format("{0},{1}{2}", line, codes, Environment.NewLine));
}
string filePath = @"E:\\LineSetting2.txt";
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.Write(sb.ToString());
}
MessageBox.Show("We are configurated successfully!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
VB.Net
Private Sub From1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
dataGridView.AllowUserToAddRows = False
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Line"),
New DataColumn("CGTypes"),
New DataColumn("Codes")})
dt.Rows.Add("Line1", "IM08+IU08+IM99+IU16", "20,14,A")
dt.Rows.Add("Line2", "IM08+IU08+IM99+IU16", "20,14,A")
dt.Rows.Add("Line3", "IM08+IU08+IM89+IU14", "20,14,A")
dataGridView.DataSource = dt
End Sub
Private Sub OnSave(sender As Object, e As EventArgs) Handles btnSelect.Click
Dim sb As StringBuilder = New StringBuilder()
For Each row As DataGridViewRow In dataGridView.Rows
Dim line As String = row.Cells(0).FormattedValue.ToString()
Dim codes As String = row.Cells(2).FormattedValue.ToString()
sb.Append(String.Format("{0},{1}{2}", line, codes, Environment.NewLine))
Next
Dim filePath As String = "E:\\LineSetting2.txt"
Using Write As StreamWriter = New StreamWriter(filePath)
Write.Write(sb.ToString())
End Using
MessageBox.Show("We are configurated successfully!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Screenshot