Auto number increment

DRGAGI 146 Reputation points
2020-12-03T21:20:23.24+00:00

Hello everyone, i have issues with auto increment in DataGridView row count. As i adding columns in table, i get numbers of rows in right order, but when i delete a row, for example number 4, my ID skip reordering and continue to add next number of the row...any suggestion?44916-snap1.jpg

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,640 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2020-12-04T03:20:42.487+00:00

    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.
    44980-gif.gif

    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.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,286 Reputation points
    2020-12-04T12:49:03.553+00:00

    Hello @DRGAGI

    When working with a DataGridView the best option is to set the DataSource be it a DataTable or a List(Of T). About the only time to not use a DataSource is for viewing information/data where information/data will not be modified.

    For example, using a DataTable subscribe to the RowDeleted event and in this event reorder the primary key.

    Here the primary key is read-only so when reordering I set the ReadOnly property to false so the values can be set then change the ReadOnly to True again

    Here I delete the row with Joe and then reorder the primary key.

    45139-1111.png

    Public Class Form1  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            Dim dt = GetData()  
      
            AddHandler dt.RowDeleted, AddressOf RowDeleted  
            DataGridView1.DataSource = dt  
      
        End Sub  
    
        Private Sub RowDeleted(sender As Object, e As DataRowChangeEventArgs)  
      
            Dim dt = CType(sender, DataTable)  
      
            If dt.Rows.Count > 0 Then  
      
                dt.Columns(0).ReadOnly = False  
      
                For index As Integer = 0 To dt.Rows.Count - 1  
                    dt.Rows(index).SetField(Of Integer)("Id", index + 1)  
                Next  
      
                dt.Columns(0).ReadOnly = True  
      
            End If  
      
        End Sub  
    End Class  
    Public Module Mocked  
        Public Function GetData() As DataTable  
            Dim dt As New DataTable  
      
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Id", .DataType = GetType(Integer),  
                              .AutoIncrement = True, .AutoIncrementSeed = 1})  
      
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Name", .DataType = GetType(String)})  
      
            dt.Rows.Add(New Object() {Nothing, "Karen"})  
            dt.Rows.Add(New Object() {Nothing, "Jim"})  
            dt.Rows.Add(New Object() {Nothing, "Joe"})  
            dt.Rows.Add(New Object() {Nothing, "Bob"})  
            dt.Rows.Add(New Object() {Nothing, "Mary"})  
      
            Return dt  
      
        End Function  
    End Module  
    
    1 person found this answer helpful.

  2. DRGAGI 146 Reputation points
    2020-12-04T17:35:00.427+00:00
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'Database13DataSet.Table1' table. You can move, or remove it, as needed.
            Me.Table1TableAdapter.Fill(Me.Database13DataSet.Table1)
    
        End Sub
    
        Private Sub Table1DataGridView_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles Table1DataGridView.RowsAdded
    
            SetRowNumber(Table1DataGridView)
    
        End Sub
    
        Private Sub SetRowNumber(table1DataGridView As DataGridView)
            For Each row As DataGridViewRow In table1DataGridView.Rows
                row.HeaderCell.Value = (row.Index + 1).ToString()
            Next
    
        End Sub
    
        Private Sub saveDatabase_Click(sender As Object, e As EventArgs) Handles saveDatabase.Click
    
            Me.Validate()
            Me.Table1BindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Database13DataSet)
    
        End Sub
    
        Private Sub addNewRow_Click(sender As Object, e As EventArgs) Handles addNewRow.Click
    
            Table1BindingSource.AddNew()
    
        End Sub
    
        Private Sub deleteCurentRow_Click(sender As Object, e As EventArgs) Handles deleteCurentRow.Click
    
            For Each item As DataGridViewRow In Table1DataGridView.SelectedRows
                Table1DataGridView.Rows.RemoveAt(item.Index)
            Next
    
        End Sub
    End Class