Hi @DRGAGI ,
You can use the following function to set DataGridView's row numbers.
Private Sub SetRowNumber(ByVal dgv As DataGridView)
For Each row As DataGridViewRow In dgv.Rows
row.HeaderCell.Value = (row.Index + 1).ToString()
Next
End Sub
I make a test on my side and you can take a look.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.Add("c1")
dt.Columns.Add("c2")
dt.Rows.Add("A", 12)
dt.Rows.Add("B", 21)
dt.Rows.Add("C", 14)
dt.Rows.Add("D", 18)
DataGridView1.DataSource = dt
End Sub
Private Sub DeleteRowBtn_Click(sender As Object, e As EventArgs) Handles DeleteRowBtn.Click
For Each item As DataGridViewRow In DataGridView1.SelectedRows
DataGridView1.Rows.RemoveAt(item.Index)
Next
End Sub
Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
SetRowNumber(DataGridView1)
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, e As DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
SetRowNumber(DataGridView1)
End Sub
Private Sub SetRowNumber(ByVal dgv As DataGridView)
For Each row As DataGridViewRow In dgv.Rows
row.HeaderCell.Value = (row.Index + 1).ToString()
Next
End Sub
Result of my test.
Best Regards,
Xingyu Zhao
*
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.