473,473 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Cell validating event problem.

Bob
In a datagridview (vs2005, VB.net) I have two columns that are checkboxes. I
need to check that only one of the two can be checked. Its not permissible
to have the two selected to true, but they can both be false.
I wrote code in the cellvalidating event as follows.
Private Sub Datagrid1_CellValidating(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellValidatingEve ntArgs) Handles
Datagrid1.CellValidating

Me.Datagrid1.Rows(e.RowIndex).ErrorText = ""

If (Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_1") Or _

(Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_2") Then

If CBool(Datagrid1("Cell_1", e.RowIndex).Value) = True _

And CBool(Datagrid1("Cell_2", e.RowIndex).Value) = True Then

Me.Datagrid1.Rows(e.RowIndex).ErrorText = "Can't have both values true."

e.Cancel = True

End If

End If

End Sub

Say I checked just one checkbox Cell_1 to start, the code allows the change
to be made. If I then ckeck the second checbox (cell_2), it gives me the
error message but does not return the second checkbox I checked to its
unchecked state. I noticed that when I click on the checkbox to uncheck it
after the e.cancel executed, that click does not change its value, I can
both see in the UI that the value did not change before the cellvalidating
event is called and in the cellvalidating event itself, when I set a
breakpoint after the line that checks and verify the value of
CBool(Datagrid1("Cell_2", e.RowIndex).Value) it remains set to true.

What am I doing wrong? I press the sacep key after the e,cancel and the
message and I get the same problem.

Any help appreciated,

Bob


Jan 9 '06 #1
5 8699
Bob
Once I'm in this process after the error text has been set, evn my close
button, that has just a Me.close() in it does not work. The only way to
close the form is to click on the form close
cross in the upper right corner of the form. When I reopen it I can see that
the chages were not comitted to the database, which is fine. Its just that
my UI does not respond correctly and I can't expect my users to get out of
it that way.

Any help would be appreciated.

Bob

"Bob" <bd*****@sgiims.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
In a datagridview (vs2005, VB.net) I have two columns that are checkboxes.
I need to check that only one of the two can be checked. Its not
permissible to have the two selected to true, but they can both be false.
I wrote code in the cellvalidating event as follows.
Private Sub Datagrid1_CellValidating(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellValidatingEve ntArgs) Handles
Datagrid1.CellValidating

Me.Datagrid1.Rows(e.RowIndex).ErrorText = ""

If (Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_1") Or _

(Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_2") Then

If CBool(Datagrid1("Cell_1", e.RowIndex).Value) = True _

And CBool(Datagrid1("Cell_2", e.RowIndex).Value) = True Then

Me.Datagrid1.Rows(e.RowIndex).ErrorText = "Can't have both values true."

e.Cancel = True

End If

End If

End Sub

Say I checked just one checkbox Cell_1 to start, the code allows the
change to be made. If I then ckeck the second checbox (cell_2), it gives
me the error message but does not return the second checkbox I checked to
its unchecked state. I noticed that when I click on the checkbox to
uncheck it after the e.cancel executed, that click does not change its
value, I can both see in the UI that the value did not change before the
cellvalidating event is called and in the cellvalidating event itself,
when I set a breakpoint after the line that checks and verify the value of
CBool(Datagrid1("Cell_2", e.RowIndex).Value) it remains set to true.

What am I doing wrong? I press the sacep key after the e,cancel and the
message and I get the same problem.

Any help appreciated,

Bob



Jan 9 '06 #2
Hi,

"Bob" <bd*****@sgiims.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
In a datagridview (vs2005, VB.net) I have two columns that are checkboxes.
I need to check that only one of the two can be checked. Its not
permissible to have the two selected to true, but they can both be false.
I wrote code in the cellvalidating event as follows.
I think common behaviour is so that the user can't leave the cell once
validation fails and is forced to correct it before he can leave the cell.
You can use the esc key to revert the value. But there is a problem in your
code, you can't use "DataGridView[,].Value" for both CheckBox, only for the
non-editing CheckBox, for the the editing CheckBox you need to use
e.FormattedValue.

(Note, FormattedValue is either a Boolean (ThreeState=false) or a CheckState
(ThreeState=true).

Private Sub Datagrid1_CellValidating(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellValidatingEve ntArgs) Handles
Datagrid1.CellValidating

Me.Datagrid1.Rows(e.RowIndex).ErrorText = ""
Dim otherCheck As Boolean = False

If (Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_1") Then
otherCheck = CBool(Datagrid1("Cell_2", e.RowIndex).Value)
End If

If (Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_2") Then
otherCheck = CBool(Datagrid1("Cell_1", e.RowIndex).Value)
End If

If ( otherCheck And CBool(e.FormattedValue) ) Then
Me.Datagrid1.Rows(e.RowIndex).ErrorText = "Can't have both values true."
e.Cancel = True
End If

End Sub
HTH,
Greetings


End If

End Sub

Say I checked just one checkbox Cell_1 to start, the code allows the
change to be made. If I then ckeck the second checbox (cell_2), it gives
me the error message but does not return the second checkbox I checked to
its unchecked state. I noticed that when I click on the checkbox to
uncheck it after the e.cancel executed, that click does not change its
value, I can both see in the UI that the value did not change before the
cellvalidating event is called and in the cellvalidating event itself,
when I set a breakpoint after the line that checks and verify the value of
CBool(Datagrid1("Cell_2", e.RowIndex).Value) it remains set to true.

What am I doing wrong? I press the sacep key after the e,cancel and the
message and I get the same problem.

Any help appreciated,

Bob



Jan 9 '06 #3
Bob
I tried to use the cellvalidating event on another column. What I realize
now is the the e.cancel will prevent the cell from bieng changed or pushed
to the unbderlying database but even when user changes cell back to what it
should be or presses esc and cell comes bacj the error icon remains in the
row selector. You can't change row you're stuck. I,m going to make a really
simple sample with a realy small Access database and post it here soon.

Bob

"Bob" <bd*****@sgiims.com> wrote in message
news:O2*************@TK2MSFTNGP12.phx.gbl...
Once I'm in this process after the error text has been set, evn my close
button, that has just a Me.close() in it does not work. The only way to
close the form is to click on the form close
cross in the upper right corner of the form. When I reopen it I can see
that the chages were not comitted to the database, which is fine. Its just
that my UI does not respond correctly and I can't expect my users to get
out of it that way.

Any help would be appreciated.

Bob

"Bob" <bd*****@sgiims.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
In a datagridview (vs2005, VB.net) I have two columns that are
checkboxes. I need to check that only one of the two can be checked. Its
not permissible to have the two selected to true, but they can both be
false.
I wrote code in the cellvalidating event as follows.
Private Sub Datagrid1_CellValidating(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellValidatingEve ntArgs) Handles
Datagrid1.CellValidating

Me.Datagrid1.Rows(e.RowIndex).ErrorText = ""

If (Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_1") Or _

(Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_2") Then

If CBool(Datagrid1("Cell_1", e.RowIndex).Value) = True _

And CBool(Datagrid1("Cell_2", e.RowIndex).Value) = True Then

Me.Datagrid1.Rows(e.RowIndex).ErrorText = "Can't have both values true."

e.Cancel = True

End If

End If

End Sub

Say I checked just one checkbox Cell_1 to start, the code allows the
change to be made. If I then ckeck the second checbox (cell_2), it gives
me the error message but does not return the second checkbox I checked to
its unchecked state. I noticed that when I click on the checkbox to
uncheck it after the e.cancel executed, that click does not change its
value, I can both see in the UI that the value did not change before the
cellvalidating event is called and in the cellvalidating event itself,
when I set a breakpoint after the line that checks and verify the value
of CBool(Datagrid1("Cell_2", e.RowIndex).Value) it remains set to true.

What am I doing wrong? I press the sacep key after the e,cancel and the
message and I get the same problem.

Any help appreciated,

Bob




Jan 9 '06 #4
Bob
Bart, I adapted your code to my datagridview, works like a charm. I'm going
to revisit the sample mdb that I sent to the newsgroup, I still wonder why
that did not work.
Oh by the way, I had to add an if condition to do the testing in the
cellvalidating only when either of these two checkbox fields gets edited.
otherwise the event does a validation on other types of fields and returns
errors, but that was easy.
I owe you one,
Bob

"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:uO**************@TK2MSFTNGP11.phx.gbl...
Hi,

"Bob" <bd*****@sgiims.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
In a datagridview (vs2005, VB.net) I have two columns that are
checkboxes. I need to check that only one of the two can be checked. Its
not permissible to have the two selected to true, but they can both be
false.
I wrote code in the cellvalidating event as follows.


I think common behaviour is so that the user can't leave the cell once
validation fails and is forced to correct it before he can leave the cell.
You can use the esc key to revert the value. But there is a problem in
your code, you can't use "DataGridView[,].Value" for both CheckBox, only
for the non-editing CheckBox, for the the editing CheckBox you need to use
e.FormattedValue.

(Note, FormattedValue is either a Boolean (ThreeState=false) or a
CheckState (ThreeState=true).

Private Sub Datagrid1_CellValidating(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellValidatingEve ntArgs) Handles
Datagrid1.CellValidating

Me.Datagrid1.Rows(e.RowIndex).ErrorText = ""
Dim otherCheck As Boolean = False

If (Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_1") Then
otherCheck = CBool(Datagrid1("Cell_2", e.RowIndex).Value)
End If

If (Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_2") Then
otherCheck = CBool(Datagrid1("Cell_1", e.RowIndex).Value)
End If

If ( otherCheck And CBool(e.FormattedValue) ) Then
Me.Datagrid1.Rows(e.RowIndex).ErrorText = "Can't have both values true."
e.Cancel = True
End If

End Sub
HTH,
Greetings


End If

End Sub

Say I checked just one checkbox Cell_1 to start, the code allows the
change to be made. If I then ckeck the second checbox (cell_2), it gives
me the error message but does not return the second checkbox I checked to
its unchecked state. I noticed that when I click on the checkbox to
uncheck it after the e.cancel executed, that click does not change its
value, I can both see in the UI that the value did not change before the
cellvalidating event is called and in the cellvalidating event itself,
when I set a breakpoint after the line that checks and verify the value
of CBool(Datagrid1("Cell_2", e.RowIndex).Value) it remains set to true.

What am I doing wrong? I press the sacep key after the e,cancel and the
message and I get the same problem.

Any help appreciated,

Bob




Jan 9 '06 #5
Hi,

"Bob" <bd*****@sgiims.com> wrote in message
news:OL**************@TK2MSFTNGP14.phx.gbl...
Bart, I adapted your code to my datagridview, works like a charm. I'm
going to revisit the sample mdb that I sent to the newsgroup, I still
wonder why that did not work.
Oh by the way, I had to add an if condition to do the testing in the
cellvalidating only when either of these two checkbox fields gets edited.
Actually i thought about this and the reasoning was that if the validating
cell isn't one of the CheckBoxCell then 'otherCheck' couldn't be True so the
following if statement would fail (because of the AND), but i forgot VB.NET
doesn't use a short-circuiting AND operator by default and therefore CBool
is used on other cells too. It should have been:

....
If ( otherCheck AndAlso CBool(e.FormattedValue) ) Then
....

Anyways, glad you got it working.

Greetings
otherwise the event does a validation on other types of fields and returns
errors, but that was easy.
I owe you one,
Bob

"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:uO**************@TK2MSFTNGP11.phx.gbl...
Hi,

"Bob" <bd*****@sgiims.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
In a datagridview (vs2005, VB.net) I have two columns that are
checkboxes. I need to check that only one of the two can be checked. Its
not permissible to have the two selected to true, but they can both be
false.
I wrote code in the cellvalidating event as follows.


I think common behaviour is so that the user can't leave the cell once
validation fails and is forced to correct it before he can leave the
cell. You can use the esc key to revert the value. But there is a problem
in your code, you can't use "DataGridView[,].Value" for both CheckBox,
only for the non-editing CheckBox, for the the editing CheckBox you need
to use e.FormattedValue.

(Note, FormattedValue is either a Boolean (ThreeState=false) or a
CheckState (ThreeState=true).

Private Sub Datagrid1_CellValidating(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellValidatingEve ntArgs) Handles
Datagrid1.CellValidating

Me.Datagrid1.Rows(e.RowIndex).ErrorText = ""
Dim otherCheck As Boolean = False

If (Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_1") Then
otherCheck = CBool(Datagrid1("Cell_2", e.RowIndex).Value)
End If

If (Datagrid1.Columns(e.ColumnIndex).DataPropertyName = "Cell_2") Then
otherCheck = CBool(Datagrid1("Cell_1", e.RowIndex).Value)
End If

If ( otherCheck And CBool(e.FormattedValue) ) Then
Me.Datagrid1.Rows(e.RowIndex).ErrorText = "Can't have both values
true."
e.Cancel = True
End If

End Sub
HTH,
Greetings


End If

End Sub

Say I checked just one checkbox Cell_1 to start, the code allows the
change to be made. If I then ckeck the second checbox (cell_2), it gives
me the error message but does not return the second checkbox I checked
to its unchecked state. I noticed that when I click on the checkbox to
uncheck it after the e.cancel executed, that click does not change its
value, I can both see in the UI that the value did not change before the
cellvalidating event is called and in the cellvalidating event itself,
when I set a breakpoint after the line that checks and verify the value
of CBool(Datagrid1("Cell_2", e.RowIndex).Value) it remains set to true.

What am I doing wrong? I press the sacep key after the e,cancel and the
message and I get the same problem.

Any help appreciated,

Bob





Jan 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dav | last post by:
I have a Winform with a derived datagrid and a button that adds a row to the datagrid in its Click event. The button has a shortcut text "&New Row". When I enter a value in a datagrid cell and...
6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
0
by: enahar | last post by:
I want to validate a cell wheather a value is greater than 0 or not.If value is greater than 0 then I want to show messagebox and focus back the same cell else move to the other cell. I am...
0
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem. The event would fire when clicking on another...
2
by: Bob | last post by:
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...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
16
by: Al Santino | last post by:
Hi, It appears displaying a messagebox in a validating event will cancel the subsequent event. In the program below, button 2's click event doesn't fire if you open a dialog box in button 1's...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
8
by: Peted | last post by:
I have an amazing problem which i think i have no hope of solving Im working with a c# dot net module that is hosted by and runs under a delphi form envrioment. Dont ask me how this insanity has...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.