B
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.DataGridViewCellValidatingEventArgs) 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
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.DataGridViewCellValidatingEventArgs) 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