AutoIncrementSeed still a problem

G

Gerry Viator

Hi all,

Ok I'm starting a new post because I'm stuck. I have included code for a
test datatable
bound to a datagrid. I want after a row is deleted for the AutoIncrement to
start from the last number
there? Not skip numbers. Please help just copy and past over a new
project, the sample will create the controls.

thanks
Gerry


Public Class Form1
Inherits System.Windows.Forms.Form

Friend MainSavedDataTable As New DataTable("Values")
Friend WithEvents Ngrid As New System.Windows.Forms.DataGrid
Friend WithEvents AddRowbtn As New System.Windows.Forms.Button
Friend WithEvents DeleteRowbtn As New System.Windows.Forms.Button

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(274, 268)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Increment Test"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load


Me.Controls.Add(Ngrid)
Me.Controls.Add(AddRowbtn)
Me.Controls.Add(DeleteRowbtn)


Ngrid.DataMember = ""
Ngrid.HeaderForeColor = System.Drawing.SystemColors.ControlText
Ngrid.Location = New System.Drawing.Point(48, 72)
Ngrid.Name = "AutoIncrement Grid test"
Ngrid.Size = New System.Drawing.Size(184, 176)
Ngrid.TabIndex = 0

AddRowbtn.Location = New System.Drawing.Point(24, 32)
AddRowbtn.Name = "AddRowbtn"
AddRowbtn.Size = New System.Drawing.Size(96, 23)
AddRowbtn.TabIndex = 1
AddRowbtn.Text = "Add DataRow"

DeleteRowbtn.Location = New System.Drawing.Point(144, 32)
DeleteRowbtn.Name = "DeleteRowbtn"
DeleteRowbtn.Size = New System.Drawing.Size(104, 23)
DeleteRowbtn.TabIndex = 2
DeleteRowbtn.Text = "Delete DataRow"


Dim i As Integer
Dim CntColumn As New DataColumn("Count")
CntColumn.DataType = GetType(Integer)
CntColumn.AutoIncrement = True
CntColumn.AutoIncrementSeed = -1
CntColumn.AutoIncrementStep = -1
MainSavedDataTable.Columns.Add(CntColumn)

For i = 1 To 5
Dim newRow As DataRow = MainSavedDataTable.NewRow
MainSavedDataTable.Rows.Add(newRow)
Next

MainSavedDataTable.AcceptChanges()
Ngrid.DataSource = MainSavedDataTable

End Sub

Private Sub AddRowbtn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles AddRowbtn.Click
Dim newRow As DataRow = MainSavedDataTable.NewRow
MainSavedDataTable.Rows.Add(newRow)
End Sub

Private Sub DeleteRowbtn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles DeleteRowbtn.Click

If MainSavedDataTable.Rows.Count > 1 Then
'Delete last row
Dim Cnt As Integer = MainSavedDataTable.Rows.Count
MainSavedDataTable.Rows(Cnt - 1).Delete()
MainSavedDataTable.AcceptChanges()


End If
End Sub
End Class
 
G

Grzegorz Danowski

U¿ytkownik "Gerry Viator said:
Hi all,

Ok I'm starting a new post because I'm stuck. I have included code for a
test datatable
bound to a datagrid. I want after a row is deleted for the AutoIncrement
to start from the last number
there? Not skip numbers. Please help just copy and past over a new
project, the sample will create the controls.

I'm not sure if you really need so feature. However, if you must, I
recommend to remove autoincrement and to make own autonumeration basing on
RowChanged event, for example:
Public Class Form1
Inherits System.Windows.Forms.Form

Friend MainSavedDataTable As New DataTable("Values")

change to:
Friend WithEvents MainSavedDataTable As New DataTable("Values")

(...)
Dim i As Integer
Dim CntColumn As New DataColumn("Count")
CntColumn.DataType = GetType(Integer)
CntColumn.AutoIncrement = True
CntColumn.AutoIncrementSeed = -1
CntColumn.AutoIncrementStep = -1

Remove AutoIncrement.

(...)

Add event handler:
Private Sub MainSavedDataTable_RowChanged(ByVal sender As Object, ByVal
e As _
System.Data.DataRowChangeEventArgs) Handles
MainSavedDataTable.RowChanged
If e.Action = DataRowAction.Add Then
Dim newId As Integer = -1

If e.Row.Table.Rows.Count <> 0 Then
newId = Me.MainSavedDataTable.Compute("Min(Count)", "") - 1
End If
e.Row("Count") = newId
End If
End Sub

End Class

I hope it helps.

Regards,
Grzegorz
 
G

Gerry Viator

Thank You very much!!

Works Great!

I'm still curious why the AutoIncrement doesn't work right after a row is
deleted and then Acceptchanges is called

oh well, thanks again
Gerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top