vendredi 22 mars 2019

Odd issue while reading checkbox state

I have a checkbox column in a datagridview.When I try to iterate through the column,the results vary on each iteration despite no changes in the checkboxes.The following is the code:

Dim TotalRows As Integer = 0

        For RW As Integer = 0 To DGVMulyo.Rows.Count - 1
            MsgBox(DGVMulyo.Rows(RW).Cells(1).Value)
            If DGVMulyo.Rows(RW).Cells(1).Value = True Then
                TotalRows = TotalRows + 1
            End If
        Next

On each iteration of the loop,the messagebox will show differing results of the state of the checkboxes.Some times even if the checkbox is checked,the output is shown as 0. I also tried the following but the same issue persisted:

        For RW As Integer = 0 To DGVMulyo.Rows.Count - 1
            MsgBox(DGVMulyo.Rows(RW).Cells(1).State)
            If DGVMulyo.Rows(RW).Cells(1).State = True Then
                TotalRows = TotalRows + 1
            End If
        Next

In the end,the following code seems to work reliably:

Dim TotalRows As Integer = 0

        For RW As Integer = 0 To DGVMulyo.Rows.Count - 1
            'MsgBox(DGVMulyo.Rows(RW).Cells(1).Value)
            If DGVMulyo.Rows(RW).Cells(1).Value = True Or DGVMulyo.Rows(RW).Cells(1).Value = 1 Then
                TotalRows = TotalRows + 1
            End If
            '  MsgBox(TotalRows)
        Next

I am using VS-2017 Community edition with Windows-7 32 Bit OS.

Khalid.




Aucun commentaire:

Enregistrer un commentaire