-
Oct 3rd, 2021, 07:38 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] CheckBox Column Issue (code working only once).
Hello!
I'm using a winforms app.
I have a DGV that looks like this...
The logic in this program is ...
If I check a cell on column 6 some operations on other cells get done, If I uncheck it other operations get also done. The operations perform well, but I have 2 problems.
1.- Operations execute only once (the time first I check and uncheck the cell on column 6), after that, if I check or uncheck the cell any number of times, nothing happens.
2.- If I uncheck cell_A, then go to check cell_B, cell_A gets checked again even when I didn't even touched it
that's spooky.
this my code:
VB.NET Code:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
With DataGridView1
Dim Chk As New DataGridViewCheckBoxCell()
Chk = CType(.Rows(.CurrentRow.Index).Cells(6), DataGridViewCheckBoxCell)
If Chk.Value Is Nothing Then
Chk.Value = False
Else
Chk.Value = True
End If
Dim Value_1 As Decimal = .Rows(.CurrentRow.Index).Cells(2).Value
Dim Value_2 As Decimal = .Rows(.CurrentRow.Index).Cells(4).Value
If Chk.Value.ToString() = "False" Then
Chk.Value = True
If .Rows(.CurrentRow.Index).Cells(3).Value = "No maneja" Then
'Do nothing
Else
.Rows(.CurrentRow.Index).Cells(5).Value = Format(Value_2 * Value_1, "c").ToString
End If
Else
Chk.Value = False
If .Rows(.CurrentRow.Index).Cells(3).Value = "No maneja" Then
'Do nothing
Else
Dim Value_3 As Decimal = .Rows(.CurrentRow.Index).Cells(3).Value
.Rows(.CurrentRow.Index).Cells(5).Value = Format(Value_2 * Value_3, "c").ToString
End If
End If
End With
End Sub
Last edited by Spybot; Oct 3rd, 2021 at 07:42 PM.
-
Oct 3rd, 2021, 09:38 PM
#2
Re: CheckBox Column Issue (code working only once).
Originally Posted by Spybot
1.- Operations execute only once (the time first I check and uncheck the cell on column 6), after that, if I check or uncheck the cell any number of times, nothing happens.
It is never the case that nothing happens. Something happens but it's not what you want or expect. If you pretend that you're just a user and just look at the application then you might not see anything happen, but that doesn't mean that nothing happens. You're not a user. You're a developer, so you need to be a developer. Put a breakpoint on the top of the event handler and then step through the code line by line to see what actually does happen.
BTW, should you actually be handling the CellContentClick event rather than the CellClick event? You should also be aware that the Value of the cell doesn't change just because the state of the check box changes. Just like other cells, the editing session has to end before the change in the cell is reflected in the Value property. I haven't looked at your code closely enough enough to know whether these are the issues but you should at least debug your code properly first, to work out exactly where and how the actual state differs from your expectations.
-
Oct 3rd, 2021, 09:39 PM
#3
Re: CheckBox Column Issue (code working only once).
You obviously have Option Strict turned off. I'd recommend turning it on now. Try this...
Code:
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
With DataGridView1
Dim Chk As DataGridViewCheckBoxCell = TryCast(.Rows(e.RowIndex).Cells(6), DataGridViewCheckBoxCell)
If Chk.Value Is Nothing Then
Chk.Value = False
Else
Chk.Value = True
End If
Dim Value_1 As Decimal = If(.Rows(e.RowIndex).Cells(2).Value IsNot Nothing, CDec(.Rows(e.RowIndex).Cells(2).Value), 0D)
Dim Value_2 As Decimal = If(.Rows(e.RowIndex).Cells(4).Value IsNot Nothing, CDec(.Rows(e.RowIndex).Cells(4).Value), 0D)
If Chk.Value = False Then
Chk.Value = True
If .Rows(e.RowIndex).Cells(3).Value?.Tostring = "No maneja" Then
'Do nothing
Else
.Rows(e.RowIndex).Cells(5).Value = Format(Value_2 * Value_1, "c").ToString
End If
Else
Chk.Value = False
If .Rows(e.RowIndex).Cells(3).Value?.Tostring = "No maneja" Then
'Do nothing
Else
Dim Value_3 As Decimal = If(.Rows(e.RowIndex).Cells(3).Value IsNot Nothing, CDec(.Rows(e.RowIndex).Cells(3).Value), 0D)
.Rows(e.RowIndex).Cells(5).Value = Format(Value_2 * Value_3, "c").ToString
End If
End If
End With
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2021, 10:14 PM
#4
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
Hi .paul
Well turning Option strict to "On", it breaks my whole program, it doesn't even run anymore.
so I'll leave it "Off" by now, continue looking for the solution. thank you any way.
-
Oct 3rd, 2021, 10:36 PM
#5
Re: CheckBox Column Issue (code working only once).
The code I posted was a fix, but it was written to work with option strict on or off. The reason turning option strict on breaks your program is because it is written poorly and is therefore vulnerable to crashing at runtime.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2021, 10:37 PM
#6
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
Sure, I switched to the CellContentClick event(), Also I almost always set breaking points to see what going on in the code, but in this particular piece of code I see a normal flow while debugging it. I still wonder Why it only executes once?
-
Oct 3rd, 2021, 10:42 PM
#7
Re: CheckBox Column Issue (code working only once).
Originally Posted by Spybot
Hi .paul
Well turning Option strict to "On", it breaks my whole program, it doesn't even run anymore.
so I'll leave it "Off" by now, continue looking for the solution. thank you any way.
That's because you've written so much bad code. Better you turn it On and fix that bad code now because there's every chance that it will be the reason for this and other issues.
In theory, the only thing you should have to do to fix any of the issues raised by turning Option Strict On is either cast or convert references or values to the actual type expected. That's very easy so refusing to do it is just being lazy. If you ever have to do more than just cast or convert then it means that there's a genuine issue with your code and you should really be thankful that it was pointed out to you. An example of the sort of thing that might be flagged is this:
vb.net Code:
Dim number As Integer = 100
numberLabel.Text = number
The Text property of a Label is type String, so you need to assign a String. If you assign an Integer then that must be implicitly converted to a String, which is not allowed with Option Strict On. The solution is to do explicitly what you are expecting to be done implicitly:
vb.net Code:
Dim number As Integer = 100
numberLabel.Text = number.ToString()
That's an example of a conversion. An example of a cast might be this:
vb.net Code:
nameLabel.Text = myDataRow("Name")
In that case, the Item property of a DataRow returns an Object reference, regardless of the type of the underlying data. If the underlying data is actually a String and you want to use it as a String, cast it as that type:
vb.net Code:
nameLabel.Text = CStr(myDataRow("Name"))
If you haven't written garbage code then this is all you will have to do to fix the issues flagged by Option Strict On. If you have written garbage code then better you fix it now and don't let incorrect behaviour sneak through to run time. You can waste a lot of time trying to fix such errors when having Option Strict On would stop you making them in the first place. It's an investment.
-
Oct 3rd, 2021, 10:44 PM
#8
Re: CheckBox Column Issue (code working only once).
Originally Posted by Spybot
I still wonder Why it only executes once?
I'd have to do some investigation to determine specifics but it may that a CellClick event is not raised while an editing session is live.
-
Oct 3rd, 2021, 11:01 PM
#9
Re: CheckBox Column Issue (code working only once).
What JM said, but specifically probably CurrentRow affects which row is ticked. Also, I don’t see why it’d only execute once
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2021, 11:04 PM
#10
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
I didn't now what "Option Strict" was for.
now I know and I'll have to fix lots of simple object type conversions.
I Guess I'll be pretty busy fixing my mess. LOL!
I'll come back to this threat when I'm done.
-
Oct 3rd, 2021, 11:12 PM
#11
Re: CheckBox Column Issue (code working only once).
Originally Posted by Spybot
I didn't now what "Option Strict" was for.
In short, to disallow late binding and implicit widening conversions. Many experienced developers lament the fact that it is Off by default but that appears to be done for two reasons. Firstly, it makes it easier for beginners to get going without having a detailed understanding of data types. For example, a beginner may think that if they want to display a number in a Label then they should just be able to assign that number to the Text property. They may not appreciate the difference between a number and a String representation of that number and getting that understanding prevents them writing code that will work unless they do something too diabolical. Secondly, it means that VB.NET works much like VB6 and thus there was less resistance from VB6 developers to migrate than if they had to fix all the late-binding and implicit conversions in their existing code.
Really though, you should turn Option Strict On for all current projects and also turn it On in the VS options, so it will be On by default for all future projects. You can then turn it Off explicitly on the very rare occasions that you actually need late binding, e.g. Office Automation. Even then, you should turn it Off at the file level and only in the files that actually require it. You should also use partial classes to keep the code in those files to an absolute minimum, thus writing the majority of your code with Option Strict On and avoiding accidentally making type-based errors.
-
Oct 3rd, 2021, 11:26 PM
#12
Re: CheckBox Column Issue (code working only once).
Originally Posted by jmcilhinney
In short, to disallow late binding and implicit widening conversions.
The reason that's a good thing is that it flags a lot of potential issues at compile time, rather than letting them get through to run time. Some errors will cause an exception to be thrown but, even worse, some may work but produce the wrong behaviour. You might have an application that appears to work but corrupts all the data is saves.
-
Oct 3rd, 2021, 11:39 PM
#13
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
I'm Fixing my entire solution, and I found this error... where it says sender.Text.IndexOf( the error text says something like: "Option Strict On, doesn't allow the link at runtime" I translated it from Spanish.
Private Sub TxtNumeric_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtNumeric.KeyPress
If (DataGridViewX1.CurrentCell.ColumnIndex > 0) Then
Dim DecimalSeparator2 As String = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator
e.Handled = Not (Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) = 8 Or (e.KeyChar = DecimalSeparator2 And sender.Text.IndexOf(DecimalSeparator2) = -1))
End If
End Sub
-
Oct 4th, 2021, 12:17 AM
#14
Re: CheckBox Column Issue (code working only once).
Code:
DirectCast(sender, TextBox).Text.IndexOf(DecimalSeparator2) = -1
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2021, 12:50 AM
#15
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
Thank you very much...
I'm almost done... LOL!
-
Oct 4th, 2021, 01:54 AM
#16
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
OK, I'm done fixing the all the issues regarding Option Strict On, believe me there were more than 500 issues.
I've also implemented the fix .paul suggested, and still not working as expected. I've also tried Breaking points and didn't catched any errors. the check box cells still execute only once.
-
Oct 4th, 2021, 01:58 AM
#17
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
"Really though, you should turn Option Strict On for all current projects and also turn it On in the VS options" I promise I will.
-
Oct 4th, 2021, 02:02 AM
#18
Re: CheckBox Column Issue (code working only once).
Originally Posted by Spybot
OK, I'm done fixing the all the issues regarding Option Strict On, believe me there were more than 500 issues.
I've also implemented the fix .paul suggested, and still not working as expected. I've also tried Breaking points and didn't catched any errors. the check box cells still execute only once.
What are you expecting…
When the checkbox value = true?
When the checkbox value = false?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2021, 02:05 AM
#19
Re: CheckBox Column Issue (code working only once).
Are you even sure the following code is correct?
Code:
If Chk.Value Is Nothing Then
Chk.Value = False
Else
Chk.Value = True
End If
If Chk.Value become True then the way I see it, it will remain true always already hence it will continuously execute the "else" part of your next if statement.
-
Oct 4th, 2021, 02:27 AM
#20
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
I expect that ...
When the checkbox value = true it does some calculations
When the checkbox value = false it also does some calculations(something like undo the first calculations)
And being able to repeat many times, not only once
-
Oct 4th, 2021, 02:40 AM
#21
Re: CheckBox Column Issue (code working only once).
So, on these subsequent occasions, is the event handler not executing at all or is it executing but not producing the results you expect? If it's the latter then you should be able to tell us exactly where and how the actual behaviour of the code differs from your expectations. If you have set a breakpoint and stepped through the code line by line then, before each step, you should be asking yourself exactly what it is that you expect to happen and then, after the step, you should be checking the state to see if that did happen. If it didn't then you have found an issue and you can describe the exact situation to us.
If the event is not being raised then that's a different matter. In that case, what you should be doing is testing. Create a new project that is a simple as it possibly can be while demonstrating the functionality at issue. If it works as expected then you can start changing it bit by bit until it either does what you want successfully or breaks. If you still can't fix the issue, you can then provide us with this bare-bones demo so that we can test for ourselves without the distraction of all the irrelevant stuff in your original project. This is how software development works. This is the stuff you should be doing before posting a question here.
Last edited by jmcilhinney; Oct 4th, 2021 at 03:01 AM.
-
Oct 4th, 2021, 02:45 AM
#22
Re: CheckBox Column Issue (code working only once).
Code:
If Chk.Value Is Nothing OrElse Chk.Value = False Then
Chk.Value = False
Else
Chk.Value = True
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2021, 02:58 AM
#23
Re: CheckBox Column Issue (code working only once).
That can be reduced to…
Code:
If Chk.Value Is Nothing Then
Chk.Value = False
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2021, 03:18 AM
#24
Re: CheckBox Column Issue (code working only once).
You need to be careful when retrieving values from the DGV. If you try to cast the value object as a decimal, it must contain a decimal and that’s not a certainty. This is a safer method than CDec. In this form it won’t work, due to your currency specifier. I’ll look up the right overload for you..
Code:
Dim v as Decimal
Decimal.TryParse(.Rows(e.RowIndex).Cells(index).Value?.ToString, v)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2021, 03:21 AM
#25
Re: CheckBox Column Issue (code working only once).
Code:
Dim v as Decimal
Decimal.TryParse(.Rows(e.RowIndex).Cells(index).Value?.ToString, Globalization.NumberStyles.AllowCurrencySymbol, Globalization.CultureInfo.CurrentCulture, v)
Last edited by .paul.; Oct 4th, 2021 at 03:39 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2021, 12:27 PM
#26
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
OK, problem # 1 has been solved!
Now I can check or un check any CheckBox cell and it works as expected (no matter how many times I click on that CheckBox cell).
This is the new code:
vb.net Code:
With DataGridView1
Dim Chk As New DataGridViewCheckBoxCell
Chk = TryCast(.Rows(e.RowIndex).Cells(6), DataGridViewCheckBoxCell)
If Chk.Value Is Nothing Or CStr(Chk.Value) = "" Then
Chk.Value = False
End If
Dim Value_1 As Decimal = CDec(.Rows(e.RowIndex).Cells(2).Value)
Dim Value_2 As Decimal = CDec(.Rows(e.RowIndex).Cells(4).Value)
If Chk.Value Is Nothing Or CStr(Chk.Value) = "" Or CBool(Chk.Value) = False Then
Chk.Value = True
If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
'Do nothing
Else
Dim Value_3 As Decimal = CDec(.Rows(e.RowIndex).Cells(3).Value)
.Rows(e.RowIndex).Cells(5).Value = Format(Value_2 * Value_3, "c").ToString
End If
ElseIf CBool(Chk.Value) = True Then
Chk.Value = False
If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
'Do nothing
Else
.Rows(e.RowIndex).Cells(5).Value = Format(Value_2 * Value_1, "c").ToString
End If
End If
End With
But problem # 2 still happening.
I can't have all the CheckBoxes unchecked, because as soon as I click on a CheckBox cell, any other unchecked cell, gets checked.
Last edited by Spybot; Oct 4th, 2021 at 12:38 PM.
-
Oct 4th, 2021, 02:36 PM
#27
Re: CheckBox Column Issue (code working only once).
Which event are you using?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2021, 02:59 PM
#28
Re: CheckBox Column Issue (code working only once).
I don't see how you can uncheck all the boxes when you have this line,
Code:
If Chk.Value Is Nothing Or CStr(Chk.Value) = "" Or CBool(Chk.Value) = False Then
Chk.Value = True
-
Oct 4th, 2021, 06:13 PM
#29
Re: CheckBox Column Issue (code working only once).
That code is nonsensical. You have this:
Code:
If Chk.Value Is Nothing Or CStr(Chk.Value) = "" Then
Chk.Value = False
followed by this:
Code:
If Chk.Value Is Nothing Or CStr(Chk.Value) = "" Or CBool(Chk.Value) = False Then
Chk.Value = True
This is what happens when you write code without knowing what that code actually has to do first. Pick up a pen and paper and write down the actual logic you're trying to implement, then write code to implement that logic specifically. If your logic is actually logical, there's no way you could check for an empty cell and, if found, set it to false, then check for an empty cell again.
-
Oct 4th, 2021, 08:40 PM
#30
Thread Starter
Hyperactive Member
Re: CheckBox Column Issue (code working only once).
OK, problem #2 SOLVED!
the new code...
vb.net Code:
With DataGridView1
Dim Value_1 As Decimal = CDec(.Rows(e.RowIndex).Cells(4).Value)
Dim Value_2 As Decimal = CDec(.Rows(e.RowIndex).Cells(2).Value)
Dim Chk As New DataGridViewCheckBoxCell
Chk = TryCast(.Rows(e.RowIndex).Cells(6), DataGridViewCheckBoxCell)
If CBool(Chk.Value) = False Then
Chk.Value = False
End If
If CBool(Chk.Value) = False Then
Chk.Value = True
If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
'Do nothing
Else
Dim Value_3 As Decimal = CDec(.Rows(e.RowIndex).Cells(3).Value)
.Rows(e.RowIndex).Cells(5).Value = Format(Value_1 * Value_3, "c").ToString
End If
Else
Chk.Value = False
If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
'Do nothing
Else
.Rows(e.RowIndex).Cells(5).Value = Format(Value_1 * Value_2, "c").ToString
End If
End If
End With
Thank you all for your help.
-
Oct 5th, 2021, 03:43 AM
#31
Re: [RESOLVED] CheckBox Column Issue (code working only once).
What if .Rows(e.RowIndex).Cells(4).Value = "hello"???
What if .Rows(e.RowIndex).Cells(4).Value = ""???
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2021, 03:45 AM
#32
Re: [RESOLVED] CheckBox Column Issue (code working only once).
CBool(Chk.Value)???
Chk.Value is a Boolean
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2021, 05:19 AM
#33
Re: [RESOLVED] CheckBox Column Issue (code working only once).
Originally Posted by .paul.
CBool(Chk.Value)???
Chk.Value is a Boolean
The value is a Boolean but the property is type Object, because it is inherited from DataGridViewCell and can therefore refer to any type of object at all. This is the crazy part:
Code:
If CBool(Chk.Value) = False Then
Chk.Value = False
End If
If CBool(Chk.Value) = False Then
Chk.Value = True
Who writes that code if they are implementing specific logic that they have determined beforehand? If a property is a particular value then set it to that same value, then check again for the same value and set it to a different value?
-
Oct 6th, 2021, 12:09 PM
#34
Thread Starter
Hyperactive Member
Re: [RESOLVED] CheckBox Column Issue (code working only once).
OK.
I've updated my code:
vb.net Code:
With DataGridViewX2
If e.ColumnIndex = Column7.Index And e.RowIndex = .CurrentCell.RowIndex Then
Dim CellX As DataGridViewRow
CellX = .Rows(e.RowIndex)
Dim Value_1 As Decimal = CDec(CellX.Cells(4).Value)
Dim Chk As Boolean = CType(.Rows(e.RowIndex).Cells(6).EditedFormattedValue, Boolean)
If Chk = False Then
If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
'Do nothing
Else
Dim Value_2 As Decimal = CDec(.Rows(e.RowIndex).Cells(2).Value)
.Rows(e.RowIndex).Cells(5).Value = Format(Value_1 * Value_2, "c").ToString
End If
ElseIf Chk = True Then
If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
'Do nothing
Else
Dim Value_3 As Decimal = CDec(CellX.Cells(3).Value)
CellX.Cells(5).Value = Format(Value_1 * Value_3, "c").ToString
End If
End If
End If
End With
And I think this a better way to detect a boolean in a DataGridView CheckBox Column. "in my opinion".
vb.net Code:
Dim Chk As Boolean = CType(.Rows(e.RowIndex).Cells(6).EditedFormattedValue, Boolean)
Regarding the input from user, ("", none numeric, zero, strings etc.) I've already handled those in a RowValidating() event.
BTW thanks for pointing me the Option Strict thing...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|