Question DataGridView Checkbox freeze-up issue

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

When a CheckBox is checked in a DataGridView it, amongst other things, updates an 'Amount' column.

VB.NET:
Private Sub AdHocCustomerListCheckChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        Dim vOutput As Label = RFC(AccountingMainWindow, "AdHocOutputLB")
        Dim TotalTB As TextBox = RFC(AccountingMainWindow, "TotalTB")
        Dim vAmount As Double = TotalTB.Text
        Dim vRunningTotal As Double = 0.0
        Dim vResidents As Integer = 0
        For Each row As DataGridViewRow In ADG1.Rows
                      Dim CB As DataGridViewCheckBoxCell = TryCast(row.Cells("CBCol"), DataGridViewCheckBoxCell)
            If CB IsNot Nothing AndAlso CB.Value IsNot Nothing AndAlso (DirectCast(CB.Value, Boolean)) = True Then
                row.Cells("Amount").Value = vAmount
                vResidents += 1
                vRunningTotal += vAmount
            End If           
        Next
        Dim vOutPutText As String = "Total recharge invoices " & vResidents & " in the sum of $" & Format(vRunningTotal, "###,##0.00")
        vOutput.Text = vOutPutText
    End Sub

This works fine - the problem is if the CheckBox is then unchecked the value stays the same for that row! I have tried adding an Else to the If statement with

VB.NET:
row.Cells("Amount").Value = 0

I have also added it to the start of the For statement - but regardless the App just freezes up!

Any ideas?

Thanks
 
Solution

Turns out that by checking the value first, and if it's not zero then updating it, works without any issue.

Sigh

VB.NET:
For Each row As DataGridViewRow In ADG1.Rows
            Dim CB As DataGridViewCheckBoxCell = TryCast(row.Cells("CBCol"), DataGridViewCheckBoxCell)
            If CB IsNot Nothing AndAlso CB.Value IsNot Nothing AndAlso (DirectCast(CB.Value, Boolean)) = True Then
                row.Cells("Amount").Value = vAmount
                vResidents += 1
                vRunningTotal += vAmount
            Else
                If Not row.Cells("Amount").Value = 0 Then
                    row.Cells("Amount").Value = 0
                End If
            End If
        Next
 
Back
Top