How to trap if a cancel has occurred on cell validation?

B

Bob

In a winform with a datagridview using cellvalidating event but also have a
save button that is located on a tablebindignnavigator.
The behaviour I observe is that if the cellvalidating issues a cancel = true
after the save button has been clicked, the update statements in the click
event of the save button still occur but nothing gets saved, which is OK.
Except that I pop a message box in the click event after the update
statements have been successfully executed (they're in a try catch
construct) to say Save completed.

This gives me the situation where Cellvalidating issues a false cxancels the
update, the save does not occur, but my user gets message Save succeeded!
This is the code in the cellvalidating event.

Private Sub TblCompanyDataGridView_CellValidating(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles
TblCompanyDataGridView.CellValidating
Me.TblCompanyDataGridView.Rows(e.RowIndex).ErrorText = ""

' Don't try to validate the 'new row' until finished

' editing since there

' is not any point in validating its initial value.

If TblCompanyDataGridView.Rows(e.RowIndex).IsNewRow Then Return

'This validates the closing

If TblCompanyDataGridView.Columns(e.ColumnIndex).Name = "dgvCellIsClosed"
Then

'If CType(e.FormattedValue, String) = "1" Then

If e.FormattedValue.Equals(True) Then

Dim brv As Boolean

brv =
ValidateCompanyClosing(CType(TblCompanyDataGridView.Rows(e.RowIndex).Cells.Item(0).Value,
Integer)) ' the validating procedure returns false for testing

If Not brv Then

If
System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName =
"fr" Then

Me.TblCompanyDataGridView.Rows(e.RowIndex).ErrorText = "Fermeture de
compagnie défendue, vérifiez les conditions nécessaires dans la
documentation."

Else

Me.TblCompanyDataGridView.Rows(e.RowIndex).ErrorText = "Closing forbidden,
see necessary conditions in documentation."

End If

e.Cancel = True

End If

End If

End If


End Sub

This is the code that does the updates

Private Sub SaveCompanyInfo()

Try

Me.Validate()

Me.TblCompanyBindingSource.EndEdit()

Me.TblCompanyTableAdapter.Update(Me.DsCompanyInfo.tblCompany)

Me.TblCompanyAddressesBindingSource.EndEdit()

Me.TblCompanyAddressesTableAdapter.Update(Me.DsCompanyInfo.tblCompanyAddresses)

Me.TblCompanyPhonesBindingSource.EndEdit()

Me.TblCompanyPhonesTableAdapter.Update(Me.DsCompanyInfo.tblCompanyPhones)

If
System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName =
"fr" Then

MsgBox("Sauvegarde réussie!")

Else

MsgBox("Save completed!")

End If

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Bfore I execute the msgbox, I should know if a cancel has been issued by any
cellvalidating events in any of the datagridviews on te form- there are two
others.

How do i determine that. Ther does not seem to be an event in any of the
datasets or the datagridviews that indicates that a cancel has occured.

Any help would be greatly appreciated.

Bob
 
B

Bart Mermuys

Hi,

Bob said:
In a winform with a datagridview using cellvalidating event but also have
a save button that is located on a tablebindignnavigator.
The behaviour I observe is that if the cellvalidating issues a cancel =
true after the save button has been clicked, the update statements in the
click event of the save button still occur but nothing gets saved,

The current (invalid) record may not be saved, but if the user had a chance
to change other records before that then those records will still be saved.
which is OK. Except that I pop a message box in the click event after the
update statements have been successfully executed (they're in a try catch
construct) to say Save completed.

This gives me the situation where Cellvalidating issues a false cxancels
the update, the save does not occur, but my user gets message Save
succeeded!
This is the code in the cellvalidating event.
[code snipped]
This is the code that does the updates

Private Sub SaveCompanyInfo()

Try

Me.Validate()

Because the BindingNavigator doesn't cause validation, it is postponed until
here, Validate() performs the last validation and returns the result.

Dim validateOK As Boolean = Me.Validate()


HTH,
Greetings
 
B

Bob

Thanks
Bart Mermuys said:
Hi,

Bob said:
In a winform with a datagridview using cellvalidating event but also have
a save button that is located on a tablebindignnavigator.
The behaviour I observe is that if the cellvalidating issues a cancel =
true after the save button has been clicked, the update statements in the
click event of the save button still occur but nothing gets saved,

The current (invalid) record may not be saved, but if the user had a
chance to change other records before that then those records will still
be saved.
which is OK. Except that I pop a message box in the click event after the
update statements have been successfully executed (they're in a try catch
construct) to say Save completed.

This gives me the situation where Cellvalidating issues a false cxancels
the update, the save does not occur, but my user gets message Save
succeeded!
This is the code in the cellvalidating event.
[code snipped]
This is the code that does the updates

Private Sub SaveCompanyInfo()

Try

Me.Validate()

Because the BindingNavigator doesn't cause validation, it is postponed
until here, Validate() performs the last validation and returns the
result.

Dim validateOK As Boolean = Me.Validate()


HTH,
Greetings
Me.TblCompanyBindingSource.EndEdit()

Me.TblCompanyTableAdapter.Update(Me.DsCompanyInfo.tblCompany)

Me.TblCompanyAddressesBindingSource.EndEdit()

Me.TblCompanyAddressesTableAdapter.Update(Me.DsCompanyInfo.tblCompanyAddresses)

Me.TblCompanyPhonesBindingSource.EndEdit()

Me.TblCompanyPhonesTableAdapter.Update(Me.DsCompanyInfo.tblCompanyPhones)

If
System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName
= "fr" Then

MsgBox("Sauvegarde réussie!")

Else

MsgBox("Save completed!")

End If

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Bfore I execute the msgbox, I should know if a cancel has been issued by
any cellvalidating events in any of the datagridviews on te form- there
are two others.

How do i determine that. Ther does not seem to be an event in any of the
datasets or the datagridviews that indicates that a cancel has occured.

Any help would be greatly appreciated.

Bob
 

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