tags:

views:

22

answers:

0

I have a parent form with a datagridview. The datagridview's first column is the Primary key of the data. Whenever the user wanted to modify a record, he must select a row from the datagridview then click on the edit button, when the edit form will be loaded.

In the edit form, i have a property called CurrentID which represents the primary key of the record.

But inspecting the CurrentID property returns a -1, and the edit code fails because there are no records with a -1 primary key.

Here is the code setting the property:

 Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click

    Dim editcustomer As New frmAddEditCustomers

    Try
        With editcustomer
            .IsAdding = False
            .CurrentID = CInt(Me.dvgCustomers.CurrentRow.Cells(0).Value) '???
            .Text = "Valiant Information Systems - Update Customer"
            .lblHeader.Text = "Update Customer"

            .txtCustomer.Text = Me.dvgCustomers.CurrentRow.Cells(1).Value
            .txtAddress.Text = Me.dvgCustomers.CurrentRow.Cells(2).Value
            .txtCity.Text = Me.dvgCustomers.CurrentRow.Cells(3).Value
            .txtLast.Text = Me.dvgCustomers.CurrentRow.Cells(8).Value
            .txtFirst.Text = Me.dvgCustomers.CurrentRow.Cells(9).Value
            .txtMiddle.Text = Me.dvgCustomers.CurrentRow.Cells(10).Value
            .txtLandline.Text = Me.dvgCustomers.CurrentRow.Cells(5).Value
            .txtCP.Text = Me.dvgCustomers.CurrentRow.Cells(6).Value
            .txtCreditLimit.Text = Me.dvgCustomers.CurrentRow.Cells(7).Value

            .ShowDialog()
        End With
    Catch ex As Exception
        MessageBox.Show(ex.Message & vbCrLf & "Please select customer to update from the list", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Try
End Sub

I can't find what's wrong with the code, setting the CurrentID property