none
How To Get DataTable Current AutoIncrement Value? RRS feed

  • Question

  • Hi Guys,

    How to get DataTable current AutoIncrement value?

    I bind DataTable to DataGridView with setting like this:

    DataTable.Columns.Add("NAME", GetType(System.String))
    DataTable.Columns.Add("SEQ", GetType(System.Int32))
    DataTable.Columns("SEQ").AutoIncrement = True
    DataTable.Columns("SEQ").AutoIncrementStep = 1
    DataTable.Columns("SEQ").Unique = True
    
    DataGridView.AutoGenerateColumns = False
    DataGridView.Columns(0).DataPropertyName = "NAME"
    DataGridView.Columns(1).DataPropertyName = "SEQ"
    DataGridView.DataSource = DataTable

    But everytime I click new row on DataGridView, it always increase value of auto number. so I want to void this by reset auto number value if user don't set any value to new row.






    • Edited by Xan To Tuesday, October 23, 2018 2:01 AM
    Tuesday, October 23, 2018 12:28 AM

All replies

  • You dont need to do this:

    DataGridView.Columns(0).DataPropertyName = "NAME"
    DataGridView.Columns(1).DataPropertyName = "SEQ"

    If .AutoGenerateColumns is set to True

    You should not name your DataGridView "DataGridView"

    When you leave a cell in your DGV you could call reject changes on the table to revert your autoinc# back


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Tuesday, October 23, 2018 12:38 AM
  • Hi Gtripodi,

    In my original code I set AutoGenerateColumns = False. I just skip the program code that was not needed in my question.

    You mean like this:

    Private Sub DataGridView_CancelRowEdit(sender As Object, e As QuestionEventArgs) Handles DataGridView.CancelRowEdit
      DataTable.RejectChanges()
    End Sub
    AutoIncrement value still increase

    Tuesday, October 23, 2018 1:40 AM
  • Hi,

    try the way

    Public Class Form1
        Dim DataTable As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DataTable.Columns.Add("NAME", GetType(System.String))
            DataTable.Columns.Add("SEQ", GetType(System.Int32))
    
            DataGridView1.DataSource = DataTable
    
        End Sub
    
        Private Sub DataGridView1_CurrentCellChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellChanged
            DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = DataGridView1.CurrentRow.Index + 1
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 23, 2018 1:43 AM
  • Hi Alex,

    I can't do that, because

    DataTable.Columns("SEQ").Unique = True

    Sorry I don't give you complete code


    • Edited by Xan To Tuesday, October 23, 2018 1:59 AM
    Tuesday, October 23, 2018 1:57 AM
  • Hi Xan,

    What is DataGridView.Columns(1).Unique?

    If I understand correctly, please see the code below.

    Public Class Form1
        Dim DataTable As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataTable.Columns.Add("NAME", GetType(System.String))
            Dim column As New DataColumn
            column.Unique = True
            column.ColumnName = "SEQ"
            DataTable.Columns.Add(column)
            DataGridView1.DataSource = DataTable
    
    
        End Sub
    
        Private Sub DataGridView1_CurrentCellChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellChanged
    
            DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = DataGridView1.CurrentRow.Index + 1
    
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 23, 2018 2:13 AM
  • Sorry Alex, I have mistyped, but I already correct that before your reply.

    As I said before, I can't use your code because if user delete row in the middle of rows, it will duplicate SEQ value


    • Edited by Xan To Tuesday, October 23, 2018 2:44 AM
    Tuesday, October 23, 2018 2:44 AM
  • Hi,

    I don't think my code wii duplicate SEQ value when user delete row in the middle of rows

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 31, 2018 7:54 AM
  • From your example above, then try to delete name a or b after you add 3 row

    Best regard

    Xan To


    • Edited by Xan To Thursday, November 22, 2018 2:14 AM
    Thursday, November 22, 2018 2:04 AM