Hi luckydead,
Add the text file in bin/Debug folder.
Plese refer below sample.
Lines.Txt
Name1|Line1
Namespaces
C#
using System.Data;
using System.Drawing;
using System.IO;
VB.Net
Imports System.Data
Imports System.Drawing
Imports System.IO
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
string[] Lines = File.ReadAllLines(Application.StartupPath + @"\Lines.txt");
foreach (string line in Lines)
{
TabPage dynamicTab = new TabPage();
DataGridView btn = new DataGridView();
btn.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
btn.DefaultCellStyle.BackColor = Color.White;
btn.DefaultCellStyle.ForeColor = Color.Black;
btn.DefaultCellStyle.SelectionBackColor = Color.FromArgb(0, 177, 89);
btn.DefaultCellStyle.SelectionForeColor = Color.White;
btn.RowHeadersVisible = true;
btn.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
btn.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
btn.Anchor = AnchorStyles.Top & AnchorStyles.Left & AnchorStyles.Right;
btn.ScrollBars = ScrollBars.Both;
btn.Dock = DockStyle.Fill;
btn.Columns.Add("SAPCenter", "Name");
btn.Columns.Add("GetLine", "Line");
btn.Columns["GetLine"].ReadOnly = true;
btn.AllowUserToAddRows = true;
btn.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(OnEditingControlShowing);
tabControl1.TabPages.Add(dynamicTab);
dynamicTab.Controls.Add(btn);
}
}
private void OnEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
((DataGridViewTextBoxEditingControl)e.Control).KeyUp += new KeyEventHandler(OnTextBoxKeyUp);
}
private void OnTextBoxKeyUp(object sender, KeyEventArgs e)
{
int index = ((DataGridViewTextBoxEditingControl)(sender)).EditingControlRowIndex;
DataGridView dgv = (((DataGridViewTextBoxEditingControl)(sender))).EditingControlDataGridView;
if (!string.IsNullOrEmpty(((DataGridViewTextBoxEditingControl)(sender)).EditingControlFormattedValue.ToString()))
{
dgv.Rows[index].Cells[1].Value = string.Format("Line{0}", index + 1);
}
else
{
dgv.Rows[index].Cells[1].Value = string.Empty;
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim Lines As String() = File.ReadAllLines(Application.StartupPath & "\Lines.txt")
For Each line As String In Lines
Dim dynamicTab As TabPage = New TabPage()
Dim btn As DataGridView = New DataGridView()
btn.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
btn.DefaultCellStyle.BackColor = Color.White
btn.DefaultCellStyle.ForeColor = Color.Black
btn.DefaultCellStyle.SelectionBackColor = Color.FromArgb(0, 177, 89)
btn.DefaultCellStyle.SelectionForeColor = Color.White
btn.RowHeadersVisible = True
btn.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
btn.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
btn.Anchor = AnchorStyles.Top And AnchorStyles.Left And AnchorStyles.Right
btn.ScrollBars = ScrollBars.Both
btn.Dock = DockStyle.Fill
btn.Columns.Add("SAPCenter", "Name")
btn.Columns.Add("GetLine", "Line")
btn.Columns("GetLine").[ReadOnly] = True
btn.AllowUserToAddRows = True
AddHandler btn.EditingControlShowing, AddressOf OnEditingControlShowing
tabControl1.TabPages.Add(dynamicTab)
dynamicTab.Controls.Add(btn)
Next
End Sub
Private Sub OnEditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
AddHandler (CType((e.Control), DataGridViewTextBoxEditingControl)).KeyUp, AddressOf OnTextBoxKeyUp
End Sub
Private Sub OnTextBoxKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
Dim index As Integer = CType((sender), DataGridViewTextBoxEditingControl).EditingControlRowIndex
Dim dgv As DataGridView = CType((sender), DataGridViewTextBoxEditingControl).EditingControlDataGridView
If Not String.IsNullOrEmpty(CType((sender), DataGridViewTextBoxEditingControl).EditingControlFormattedValue.ToString()) Then
dgv.Rows(index).Cells(1).Value = String.Format("Line{0}", index + 1)
Else
dgv.Rows(index).Cells(1).Value = String.Empty
End If
End Sub
Screenshot