Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding sources & Remove Current

Status
Not open for further replies.

74Stag

Programmer
Aug 17, 2004
9
GB
Hi

I am having some problems with a windows form I am developing as part of a database application. The form needs to hold an address history for a person.

The main part of the form has bound text boxes etc showing the details of the current record. I have also added a datagridview which is bound to a table built using code and placed in a seperate dataset. The datagridview is populated using a query which shows the details for any other records in the address history thus giving the user an easy to read history.

My plan is that when the user double clicks on a row in the datagridview it will open that record in the main area of the form. (I hope this is making sense!)

There is a problem however. I would like the form to open with a blank new record so the user can quickly add a new record if they need to. However when the user then double clicks on the datagridview to open a previous record it throws an error complaining about a null field. I guess this is because the table the form is bound to has non-null fields.

I though I'd get around this by using the .removecurrent command if the record was blank and sure enough this got me around the problem. It has however caused another strange problem which I just can't seem to solve.

To open the correct record for the user I use the .find method on the bindingsource attached to the form using the primary key. I extract the primary key from the binding source of the DGV. This works the first time but will not work after the .removecurrent command is called. It just keeps returning the index and primary key of the first record in the forms bindingsource.

I've posted the offending bit of code below and any ideas would be greatly appreciated. Thanks in advance for reading all this!

Private Sub dgvHistory_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvHistory.DoubleClick

Try

Dim intRecordID As Int32
Dim intResult As Int32

intResult = saveRecord()

'if there are no problems with saving the existing record
If (intResult = 1) Then 'let the user open a previous record after saving the current

'get the index of the record we want to move to
intRecordID = Me.ApplicantPreviousAddressBindingSource.Find("PrevAddID", bndHistory.Current("PrevAddID"))
MessageBox.Show(bndHistory.Current("PrevAddID") & " " & intRecordID)
Me.ApplicantPreviousAddressBindingSource.Position = intRecordID
initializeForm()
populateHistory()

ElseIf (intResult = 2) Then 'the current record is blank and needs discarding

'get the index of the record we want to move to
intRecordID = Me.ApplicantPreviousAddressBindingSource.Find("PrevAddID", bndHistory.Current("PrevAddID"))
'discard the current record
Me.ApplicantPreviousAddressBindingSource.RemoveCurrent()
MessageBox.Show(bndHistory.Current("PrevAddID") & " " & intRecordID)
Me.ApplicantPreviousAddressBindingSource.Position = intRecordID
initializeForm()
populateHistory()

End If

Catch ex As Exception

MessageBox.Show(ex.Message, "dgvHistory double click", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top