cara membuat nomor urut otomatis dalam datagridview vbnet

;

 

coding :

 

Public Class Form1

 

Sub Norutis()
Dim baris As Integer = DGV.RowCount - 1
If DGV.RowCount - 1 = 0 Then
DGV.Rows(baris).Cells(0).Value = 1
Else
DGV.Rows(baris).Cells(0).Value = DGV.RowCount
End If
End Sub

 

Sub Norutisdelete()
For baris As Integer = 0 To DGV.RowCount - 1
DGV.Rows(baris).Cells(0).Value = baris + 1
Next
End Sub

 

Private Sub DGV_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellEndEdit
Call Norutis()
End Sub

 

Private Sub DGV_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DGV.KeyDown
On Error Resume Next
Dim baris As Integer = DGV.RowCount - 1
If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Escape Then
DGV.Rows.Remove(DGV.CurrentRow)
Call Norutisdelete()
End If
End Sub

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
DGV.Columns(0).ReadOnly = True
Call Norutis()
End Sub

 

End Class