Results 1 to 34 of 34

Thread: [RESOLVED] CheckBox Column Issue (code working only once).

  1. #1

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    Resolved [RESOLVED] CheckBox Column Issue (code working only once).

    Hello!

    I'm using a winforms app.
    I have a DGV that looks like this...
    Name:  Captura de pantalla 2021-10-03 192327.jpg
Views: 357
Size:  15.5 KB

    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:
    1. Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    2.         With DataGridView1
    3.             Dim Chk As New DataGridViewCheckBoxCell()
    4.             Chk = CType(.Rows(.CurrentRow.Index).Cells(6), DataGridViewCheckBoxCell)
    5.  
    6.             If Chk.Value Is Nothing Then
    7.                 Chk.Value = False
    8.             Else
    9.                 Chk.Value = True
    10.             End If
    11.  
    12.             Dim Value_1 As Decimal = .Rows(.CurrentRow.Index).Cells(2).Value
    13.             Dim Value_2 As Decimal = .Rows(.CurrentRow.Index).Cells(4).Value
    14.  
    15.             If Chk.Value.ToString() = "False" Then
    16.                 Chk.Value = True
    17.                 If .Rows(.CurrentRow.Index).Cells(3).Value = "No maneja" Then
    18.                     'Do nothing
    19.                 Else
    20.                     .Rows(.CurrentRow.Index).Cells(5).Value = Format(Value_2 * Value_1, "c").ToString
    21.                 End If
    22.             Else
    23.                 Chk.Value = False
    24.                 If .Rows(.CurrentRow.Index).Cells(3).Value = "No maneja" Then
    25.                     'Do nothing
    26.                 Else
    27.                     Dim Value_3 As Decimal = .Rows(.CurrentRow.Index).Cells(3).Value
    28.                     .Rows(.CurrentRow.Index).Cells(5).Value = Format(Value_2 * Value_3, "c").ToString
    29.                 End If
    30.             End If
    31.         End With
    32.     End Sub
    Last edited by Spybot; Oct 3rd, 2021 at 07:42 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: CheckBox Column Issue (code working only once).

    Quote Originally Posted by Spybot View Post
    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.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  4. #4

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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.

  6. #6

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: CheckBox Column Issue (code working only once).

    Quote Originally Posted by Spybot View Post
    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:
    1. Dim number As Integer = 100
    2.  
    3. 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:
    1. Dim number As Integer = 100
    2.  
    3. numberLabel.Text = number.ToString()
    That's an example of a conversion. An example of a cast might be this:
    vb.net Code:
    1. 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:
    1. 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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: CheckBox Column Issue (code working only once).

    Quote Originally Posted by Spybot View Post
    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.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  10. #10

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: CheckBox Column Issue (code working only once).

    Quote Originally Posted by Spybot View Post
    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.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: CheckBox Column Issue (code working only once).

    Quote Originally Posted by jmcilhinney View Post
    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.

  13. #13

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: CheckBox Column Issue (code working only once).

    Code:
    DirectCast(sender, TextBox).Text.IndexOf(DecimalSeparator2) = -1

  15. #15

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    Re: CheckBox Column Issue (code working only once).

    Thank you very much...
    I'm almost done... LOL!

  16. #16

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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.

  17. #17

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: CheckBox Column Issue (code working only once).

    Quote Originally Posted by Spybot View Post
    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?

  19. #19
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    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.
    Regards,

    â„¢

    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  20. #20

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  22. #22
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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)

  25. #25
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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)

  26. #26

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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:
    1. With DataGridView1
    2.             Dim Chk As New DataGridViewCheckBoxCell
    3.             Chk = TryCast(.Rows(e.RowIndex).Cells(6), DataGridViewCheckBoxCell)
    4.             If Chk.Value Is Nothing Or CStr(Chk.Value) = "" Then
    5.                 Chk.Value = False
    6.             End If
    7.             Dim Value_1 As Decimal = CDec(.Rows(e.RowIndex).Cells(2).Value)
    8.             Dim Value_2 As Decimal = CDec(.Rows(e.RowIndex).Cells(4).Value)
    9.  
    10.             If Chk.Value Is Nothing Or CStr(Chk.Value) = "" Or CBool(Chk.Value) = False Then
    11.                 Chk.Value = True
    12.                 If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
    13.                     'Do nothing
    14.                 Else
    15.                     Dim Value_3 As Decimal = CDec(.Rows(e.RowIndex).Cells(3).Value)
    16.                     .Rows(e.RowIndex).Cells(5).Value = Format(Value_2 * Value_3, "c").ToString
    17.                 End If
    18.             ElseIf CBool(Chk.Value) = True Then
    19.                 Chk.Value = False
    20.                 If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
    21.                     'Do nothing
    22.                 Else
    23.                     .Rows(e.RowIndex).Cells(5).Value = Format(Value_2 * Value_1, "c").ToString
    24.                 End If
    25.             End If
    26.         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.

  27. #27
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: CheckBox Column Issue (code working only once).

    Which event are you using?

  28. #28
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    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

  29. #29
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  30. #30

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    Re: CheckBox Column Issue (code working only once).

    OK, problem #2 SOLVED!

    the new code...
    vb.net Code:
    1. With DataGridView1
    2.  Dim Value_1 As Decimal = CDec(.Rows(e.RowIndex).Cells(4).Value)
    3.             Dim Value_2 As Decimal = CDec(.Rows(e.RowIndex).Cells(2).Value)
    4.             Dim Chk As New DataGridViewCheckBoxCell
    5.             Chk = TryCast(.Rows(e.RowIndex).Cells(6), DataGridViewCheckBoxCell)
    6.  
    7.             If CBool(Chk.Value) = False Then
    8.                 Chk.Value = False
    9.             End If
    10.  
    11.             If CBool(Chk.Value) = False Then
    12.                 Chk.Value = True
    13.                 If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
    14.                     'Do nothing
    15.                 Else
    16.                     Dim Value_3 As Decimal = CDec(.Rows(e.RowIndex).Cells(3).Value)
    17.                     .Rows(e.RowIndex).Cells(5).Value = Format(Value_1 * Value_3, "c").ToString
    18.                 End If
    19.             Else
    20.                 Chk.Value = False
    21.                 If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
    22.                     'Do nothing
    23.                 Else
    24.                     .Rows(e.RowIndex).Cells(5).Value = Format(Value_1 * Value_2, "c").ToString
    25.                 End If
    26.             End If
    27. End With

    Thank you all for your help.

  31. #31
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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 = ""???

  32. #32
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] CheckBox Column Issue (code working only once).

    CBool(Chk.Value)???

    Chk.Value is a Boolean

  33. #33
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] CheckBox Column Issue (code working only once).

    Quote Originally Posted by .paul. View Post
    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?

  34. #34

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    Re: [RESOLVED] CheckBox Column Issue (code working only once).

    OK.
    I've updated my code:
    vb.net Code:
    1. With DataGridViewX2
    2.             If e.ColumnIndex = Column7.Index And e.RowIndex = .CurrentCell.RowIndex Then
    3.                 Dim CellX As DataGridViewRow
    4.                 CellX = .Rows(e.RowIndex)
    5.                 Dim Value_1 As Decimal = CDec(CellX.Cells(4).Value)
    6.                 Dim Chk As Boolean = CType(.Rows(e.RowIndex).Cells(6).EditedFormattedValue, Boolean)
    7.                 If Chk = False Then
    8.                     If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
    9.                         'Do nothing
    10.                     Else
    11.                         Dim Value_2 As Decimal = CDec(.Rows(e.RowIndex).Cells(2).Value)
    12.                         .Rows(e.RowIndex).Cells(5).Value = Format(Value_1 * Value_2, "c").ToString
    13.                     End If
    14.                 ElseIf Chk = True Then
    15.                     If .Rows(e.RowIndex).Cells(3).Value.ToString = "No maneja" Then
    16.                         'Do nothing
    17.                     Else
    18.                         Dim Value_3 As Decimal = CDec(CellX.Cells(3).Value)
    19.                         CellX.Cells(5).Value = Format(Value_1 * Value_3, "c").ToString
    20.                     End If
    21.                 End If
    22.             End If
    23.         End With

    And I think this a better way to detect a boolean in a DataGridView CheckBox Column. "in my opinion".
    vb.net Code:
    1. 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
  •  



Click Here to Expand Forum to Full Width