Home All Groups Group Topic Archive Search About

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

Author
7 Jan 2006 12:23 AM
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

Author
7 Jan 2006 1:28 AM
Bart Mermuys
Hi,

"Bob" <bduf***@sgiims.com> wrote in message
news:%23SSnXCyEGHA.1312@TK2MSFTNGP09.phx.gbl...
> 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.

Show quoteHide quote
> 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

Show quoteHide quote
>
> 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
>
>
>
>
>
>
Author
7 Jan 2006 3:09 PM
Bob
Thanks
Show quoteHide quote
"Bart Mermuys" <bmermuys.nospam@hotmail.com> wrote in message
news:O0IgXlyEGHA.3000@TK2MSFTNGP14.phx.gbl...
> Hi,
>
> "Bob" <bduf***@sgiims.com> wrote in message
> news:%23SSnXCyEGHA.1312@TK2MSFTNGP09.phx.gbl...
>> 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
>>
>>
>>
>>
>>
>>
>
>