Hi luckydead,
Please refer below sample.
NameSpaces
VB.Net
Imports System.IO
Code
VB.Net
Dim fPreviousValue As Integer = 0
Public Function ImportDataGrid(Path As String, dvg As DataGridView)
Using stream As FileStream = File.OpenRead(Path)
Using reader As New StreamReader(stream)
Dim line As String = reader.ReadLine()
While (line IsNot Nothing)
Dim columns = line.Split("|")
line = reader.ReadLine()
Dim index = dvg.Rows.Add()
dvg.Rows(index).SetValues(columns)
End While
End Using
End Using
dvg.Refresh()
Return True
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ImportDataGrid(My.Application.Info.DirectoryPath & "\Lines.txt", DataGridView1)
AddHandler DataGridView1.EditingControlShowing, AddressOf OnEditingControlShowing
NumericUpDown1.Value = CInt(DataGridView1.Rows.Count)
fPreviousValue = NumericUpDown1.Value 'Store 2
End Sub
Private Sub OnEditingControlShowing(sender As Object, 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
If index > 0 Then
Dim i As Integer = dgv.Rows.Count - 1
dgv.Rows(index).Cells(1).Value = String.Format("Line{0}", index + 1) 'increase +1
Else
dgv.Rows(index).Cells(1).Value = String.Empty 'Default start Line1
End If
End If
End Sub
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
If Me.Created Then
DataGridView1.AllowUserToAddRows = False
'Determine which way the value of the control has changed
Dim index As Integer = DataGridView1.Rows.Count
If DataGridView1.Rows.Count = 0 Then
DataGridView1.Rows.Clear()
DataGridView1.Rows.Insert(0, "NameLine1", "Line1") 'Default start Line1
ElseIf NumericUpDown1.Value >= fPreviousValue Then
'up
DataGridView1.Rows.Add(String.Format("NameLine{0}", index + 1), String.Format("Line{0}", index + 1)) 'increase +1
ElseIf NumericUpDown1.Value < fPreviousValue Then
'down
DataGridView1.Rows.RemoveAt(DataGridView1.Rows.Count - 1)
End If
fPreviousValue = NumericUpDown1.Value
DataGridView1.AllowUserToAddRows = True
End If
End Sub
Private Sub DataGridView1_UserDeletingRow(sender As Object, e As DataGridViewRowCancelEventArgs) Handles DataGridView1.UserDeletingRow
e.Cancel = True
End Sub
Screenshot